You can look up what the standard has to say about
when multiple inheritance is involved, but I'm going to live dangerously and not bother.
The first (hidden) data member of every object that has a virtual function is a
. It points to a table of function pointers, one for each virtual function in that class. That table is shared by all objects in the class, with each
pointing to it. The compiler assigns each virtual function an index into that table so that a virtual function can then invoked by
If an object belongs to a class that uses multiple inheritance, it will have more than one
, and they will appear consecutively as the first members in the object.
, it isn't a problem because
is the first one in an
. However, when you try to
, the resulting pointer would have to reference a
that is not
at the top of the object. So if you later did a
on that pointer, it would think the object started at the location of that
when it actually didn't. Returning the object's memory to the heap would cause an exception or heap corruption, because it doesn't start at the same address as the block that was originally allocated. So the standard probably says that, in the case of multiple inheritance, an object of that class can only be dynamically cast to the first
of its multiple base classes.
: If the target class (
) didn't have any virtual functions, the
would probably work: that class doesn't have
, so the pointer could still reference the top of the original object (
However, if any of
had a data member
, my guess is that the
would always fail. Why? Because to access a data member, the compiler takes a pointer to the object and adds an offset. But if there are extra
s in there because the underlying class uses multiple inheritance, that offset would be wrong.