I found myself in a situation where I know what type something is. The Type is one of three (or more) levels of inheritance. I call factory which returns B*
how
A cross-cast:
struct Base1 { virtual void f1(); };
struct Base2 { virtual void f2(); };
struct Derived : Base1, Base2 {};
Base1* b1 = new Derived();
Base2* b2 = dynamic_cast(b1);
requires use of dynamic_cast
, it cannot be done with static_cast
(static_cast
should have caused a compile-time error). dynamic_cast
will also fail if either base class is not polymorphic (the presence of virtual functions is NOT optional).
See this explanation on MSDN