Because, if Baseallowed (covariantly), you can do it that no-no:
Base Class::* ptr = &Class::member;
Class obj;
obj.*ptr = Base();
At the same time, member pointers also cannot be contravariant, because otherwise you could do it, which is also no-no:
struct Class2 {
Base member;
};
Derived Class2::* ptr2 = &Class2::member;
Class2 obj2;
obj2.member = Base();
Derived& d = obj2.*ptr2;
Thus, pointers to elements are neither covariant nor contravariant, but are invariant: the type must exactly match.
source
share