Consider this type hierarchy:
struct A {
int x;
};
struct B : A {
int y;
};
The type of &B::y
is int B::*
, but the type of &B::x
is int A::*
. It can be convertible to int B::*
, which is what I need, but I need to explicitly perform that conversion.
Is there a handy way of deducing the type to be a pointer to a member of the class I specified? After all, if I wanted an A::*
to x
I could do &A::x
.
Such conversion would not be possible in case of virtual inheritance. Assume
and two functions,
f(B*)
needs to make a vtable lookup in order to find the base pointer ofA
instance within*pB
whileg(B*)
only applies an offset topB
. Therefore the object&A::x
can not be expressed as an instance of a typeint B::*
, it needs a different runtime logic to evaluate with a pointer toB
than anint B::*
would.Even though
B
is the most derived class here and now, one might define another classC
inheriting bothA
andB
in a different compilation unit and want to pass aC*
tof()
. So the offset ofA
withinB
can not be taken for fixed.One could argue that the language could permit the conversion if no virtual inheritance is present, or in anonymous namespaces, or... but it was probably easier to disallow it consistently.