I have:
class A {...};
class B : public A {...};
class C : public B {...};
Then I’m storing C instance as void*:
C *instance = new C();
void *pC = instance;
Is it ok to make this:
B *pB = reinterpret_cast<B*>(pC);
Or I have to cast to C* only?
PS: I have more classes derived from B in my program and I’m not sure if’s ok to cast like i’m doing (to B*).
Why void*: I’m using void *userdata' field of physical body class in box2d engine. I can’t store my class there in other way
I would suggest casting it back to the original
C*then do adynamic_casttoB*in order to follow the rules of C++ – although you should avoid casting tovoid*in the first place and instead use a base pointer (A*) instead of void ptr.