$4.11/2 states -
An rvalue of type “pointer to member of
Bof type cvT,” whereBis a class type, can be converted to an rvalue of type “pointer to member ofDof type cvT,” whereDis a derived class (clause 10) ofB. IfBis an inaccessible (clause 11), ambiguous (10.2) or virtual (10.1) base class ofD, a program that necessitates this conversion is ill-formed.
My question is why we have the restriction of B not being a virtual base class of D?
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.
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