class Base; class Lock { Lock() {} friend class Base; }; class Base : virtual private Lock { public: void foo() { std::cout << "Hello world" << std::endl; } }; class Derived : public Base { }; int main() { Derived obj; obj.foo(); return 0; } 

explain what the role of virtual is ... what if you inherit from the Lock class without virtual, then the code is compiled and gives an error

note: 'Derived :: Derived ()' is implicitly deleted: it would be ill-formed: class Derived: public Base

    1 answer 1

    With virtual inheritance, the order of calling constructors is broken, and the constructor of the virtual base class Lock will be called by the class Derived . However, the class Derived does not have access to the private constructor Lock::Lock() .