Virtual inheritance was introduced primarily to solve the classic Diamond shaped Inheritance problem.
Consider the following classes:
class Base {}; class Derived1: Base {}; class Derived2: Base {}; struct MostDerived: Derived1, Derived2 {};
Class
MostDerived has 2 Base instances here because of this diamond-shaped hierarchy.
To solve this problem, C ++ uses the virtual and introduces the concept of Virtual Inheritance .
Thus, adding the virtual here, like:
class Derived1: virtual Base {}; class Derived2: virtual Base {};
Ensures that there will now be only one Base instance in the MostDerived class.
And the MostDerived class creates an instance of the Base class, calling its constructor.
With the background above (in bold), consider the following code example:
Usable class is obtained practically from Usable_lock , therefore the derived class Usable MUST creates an instance of the base class Usable_lock object, calling its constructor.
But the Usable_lock class has a private constructor, so only the class itself can access the constructor, thereby preventing other classes from being extracted from it.
Link to C ++ 03 standard:
Section 12.6.2 Initialization of Databases and Members
Item 6:
All sub-objects representing virtual base classes are initialized by the constructor of the derived class itself (1.8). If the constructor of the derived class itself does not specify a mem initializer for the virtual base class V, then the default constructor Vs. is called to initialize the subobject of the virtual base class. If V does not have an available default constructor, initialization is poorly formed. The initializer meme that names the virtual base class is ignored when the constructor of any class that is not the most derived class is executed.