Multiple inheritance casting from base class to different derived class

The way to go from any type to any other is dynamic_cast. But it requires the object to be polymorphic. In general this requires a v-table to be associated to both A and B, so: if A and B have at least one virtual function, and RTTI is not disable,

A* pa1 = new C;
A* pa2 = new A;

B* pb1 = dynamic_cast<B*>(pa1);
B* pb2 = dynamic_cast<B*>(pa2);

will result in pb2 to be null, and pb1 to point to the B part of the object containing *pa1 as its A part. (The fact it's C or whatever other derived from those two bases doesn't matter).

Otherwise, where all needs to be static, you have to go through C

B* pb = static_cast<B*>(static_cast<C*>(pa));

Note that static_cast<B*>(pA) cannot compile, being A and B each other unrelated.


No. This is not possible (direct casting from A* to B*).

Because the address of A and B are at different locations in class C. So the cast will be always unsafe and possibly you might land up in unexpected behavior. Demo.

The casting should always go through class C. e.g.

A* pa = new C();
B* pb = static_cast<C*>(pa);
                   ^^^^ go through class C

Demo