If you have a void* pointer to Derived class that inherits from both BaseA and BaseB, how does the compiler cast the void* pointer to BaseA* (or BaseB*) without knowing that the void* pointer is of type Derived?
If you have a void* pointer to Derived class that inherits from both BaseA
Share
It doesn’t. The only guarantee when casting to and from a
void*using astatic_castis:For example, the following code is incorrect because the
void*is cast to a type other than the original pointer type (the cast sequence isB1*->void*->B2*):Attempting to use
b2ptrhere would result in undefined behavior. The only type to which you can safely castvoidptrisB1*, since that is the type from which thevoid*was obtained (well, or to achar*, since anything can be accessed via achar*).