This has nothing to do with overriding functions. It has to do with conversions. It really doesn't have to do with accessibility (i.e "private" or such) directly either. Here is a simpler example
struct A { int a; };
struct B : A { };
struct C : B, A { }; // direct A can't be referred to!
You can refer to the indirect A
object by first converting to B
and then to A
:
B *b = &somec;
A *a = b;
You cannot do such with the direct A object. If you try to directly convert to A
, it will have two possibilities. It follows that it is impossible to refer to the non-static data members of the direct A
object given a Derived
object.
Notice that accessibility is orthogonal to visibility. Something can be accessible even tho it's not visible (for example by refering to it by a qualified name), and something can be visible even though it's not accessible. Even if all the above derivations would be declared private
, the problem would still show up: Access is checked last - it won't influence name lookup or conversion rules.
Also, anyone can cast to an unambiguous private base class with defined behavior (the C++ Standard makes an exception for this) using a C-style cast, even if normally access wouldn't be granted to do so. And then there are still friends and the class itself that could freely convert.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…