Consider a situation involving a non-virtual base class:
class A { int a; }
class B : public A { int b; }
class C : public A { int c; }
class D : public B, public C { int d; }
Here's a possible memory layout:
+-------------+
| A: int a; |
+-------------+
| B: int b; |
+-------------+
| A: int a; |
+-------------+
| C: int c; |
+-------------+
| D: int d; |
+-------------+
D
ends up with two A
subobjects because it inherits from B
and C
and both each have an A
subobject.
Pointers to member variables are typically implemented as an integer offset from the start of the object. In this case, the integer offset for int a
in an A
object is zero. So a "pointer to int a
of type A
" may be simply an integer offset of zero.
To convert a "pointer to int a
of type A
" to a "pointer to int a
of type B
," you just need an integer offset to the A
subobject located in B
(the first A
subobject).
To convert a "pointer to int a
of type A
" to a "pointer to int a
of type C
," you just need an integer offset to the A
subobject located in C
(the second A
subobject).
Since the compiler knows where B
and C
is relative to A
, the compiler has enough information on how to downcast from A
to B
or C
.
Now consider a situation involving a virtual base class:
struct A { int a; }
struct B : virtual public A { int b; }
struct C : virtual public A { int c; }
struct D : public B, public C { int d; }
Possible memory layout:
+-------------+
| B: ptr to A | ---+
| int b; | |
+-------------+ |
| C: ptr to A | ---+
| int c; | |
+-------------+ |
| D: int d; | |
+-------------+ |
| A: int a; | <--+
+-------------+
Virtual base classes are typically implemented by having B
and C
(which virtually derive from A
) contain a pointer to the single A
subjobject. The pointers to the A
subobject are required because the location of A
relative to B
and C
is not constant.
If all we had was a "pointer to int a
of type A
," we won't be able to cast it to a "pointer to int a
of type B
", since the location of the B
and C
subobjects can vary relative to A
. A
doesn't have back-pointers to B
nor C
, so we simply don't have enough information for the downcast to work.