1

To avoid inheritance of class Base, I ran a test:

class non_inherit {
protected:
    non_inherit(){}
};

class Base : virtual non_inherit{
public:
    Base(){}
};

class Derived : public Base{
public:
    Derived(){}
};

int main(){
    Derived d;
    return 0;
}

And it runs successfully with no errors. Since Base is privately inherited from non_inherit, the default constructor is supposed to be private for Derived, so how can Derived access the constructor of non_inherit?

I tried to call the c'tor of non_inherit explicitly in the c'tor of Derived, and it fails! So what did the compiler do to successfully create the object of Derived.

刘卫东
  • 325
  • 1
  • 7
  • Possible duplicate of [How constructor works in private inheritance](https://stackoverflow.com/questions/31357515/how-constructor-works-in-private-inheritance) –  Nov 15 '18 at 13:06
  • Not really a duplicate: 31357515 explains why `Base` can use the `non_inherit` constructor, but doesn't address `Derived`. – aschepler Nov 15 '18 at 13:18
  • MSVC actually rejects the code. – aschepler Nov 15 '18 at 13:24

0 Answers0