One virtual inheritance

I understand that virtual inheritance of a base class creates a common common base class among several derived classes, thus eliminating the DDD problem. If I have only one derived class for my base class, is there a difference when I inherit a database practically or not virtually? Basically, I am trying to understand the explanation provided in the query. Can I deny the output from the class at compile time? where the base class Usage_lock is inherited almost to prevent deductions from the class. If I delete this virtual key, the behavior changes, that is, I can get subclasses from Usable. Therefore, I want to understand the reason for the difference in the virtual key in single inheritance scenarios.

+4
source share
3 answers

The main difference between one case of virtual inheritance is that only the most derived class calls the constructor of almost inherited databases, and all other classes are provided with a link to the constructed class (this happens behind the scenes).

So, in this example, since trying to get Usable further requires that the new class call the Usable_lock constructor (which is private), it is not possible for any other classes to be obtained from Usable , only Usable allowed to create a lock object because it is friend lock.

+5
source

The virtual base class will be built by the most derived class. Having actually received and made the designer of such a base private, there is no other way to build it, therefore, it is effective to prevent the conclusion. However, its rather artificial design, and it also has some overhead.

+2
source

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.

+2
source

All Articles