I have the following case in C++:
Abstract1
and Abstract2
. They are unrelated.Foo
deriving from both Abstract1
and Abstract2
I am in a compilation unit where I have no information about class Foo
(no declaration, no definition). Only Abstract1
and Abstract2
are known.
(Actually, Foo is even defined in a DLL)
Will dynamic_cast allow casting from Abstract1*
to Abstract2*
? Is this a standard?
What you describe is a so-called cross-cast. For dynamic_cast<T>(v)
, the standard specifies in [expr.dynamic.cast]/8
If
C
is the class type to whichT
points or refers, the run-time check logically executes as follows:
If, in the most derived object pointed (referred) to by
v
,v
points (refers) to a public base class subobject of aC
object [..]Otherwise, if
v
points (refers) to apublic
base class subobject of the most derived object, and the type of the most derived object has a base class, of typeC
, that is unambiguous andpublic
, the result points (refers) to theC
subobject of the most derived object.
That will work even with no information about Foo
's existence in the translation unit that contains the cast.
You should check out this question too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With