Let's say we have a library that we're using that is meant for us to derive some types from:
class A {};
class B : public A {};
class C : public B {};
And when we're deriving our types, we have some things that are common to all of our cases:
class CommonStuff {};
class D : public CommonStuff, public C {};
Now, as we're working with our library, and there is a callback that takes type A& (or B& or C&)
void some_func(A& obj);
And assume in that function it expects polymorhpic behavior, but we need to access some of our CommonStuff:
void some_func(A& obj)
{
CommonStuff& comn = dynamic_cast<CommonStuff&>(obj);
}
Since there's no direct correlation between A and CommonStuff, we cannot use static_cast
, reinterpret_cast
is obviously not the right choice as it will introduce slicing. The only option here is dyanmic_cast
.
Now, take this with a grain of salt, because this could be worked around.