'Inaccessible direct base' caused by multiple inheritance

前端 未结 2 1986
花落未央
花落未央 2020-11-29 06:51

Spoiler alert: Maybe a stupid question. :)

#include 

using namespace std;

class Base
{
    public:
        virtual void YourMethod(int) con         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 07:07

    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.

提交回复
热议问题