Hi all, I have a problem where my derived class is losing its state data. some isolated code is below, the problem manifests itself in CClassD::DoExtras().
CClassA and CClassC exist in our old codebase and CClassB and CClassD are derived for our new variant of the codebase.
int i = b.data; // ouch! b.data is uninitialised
Do you mean dataA or dataB?
I think you should be using static_cast instead of reinterpret_cast because reinterpret cast will not handle if CClassA is stored at an later offset in CClassB.
> Unfortunately CClassB has a virtual dtor() but no virtual methods so dynamic cast fails with a compiler error.
A virtual destructor is sufficient to make the class a polymorphic type.
The dynamic down-cast from CClassA gives an error because CClassA is not a polymorphic type. Add a virtual destructor for CClassA and the error will go away.