my-server
← Wiki

Virtual inheritance

Virtual inheritance is a C++ technique that ensures only one copy of a base classs member variables are inherited by grandchild derived classes. Without virtual inheritance, if two classes <code>B</code> and <code>C</code> inherit from a class <code>A</code>, and a class <code>D</code> inherits from both <code>B</code> and <code>C</code>, then <code>D</code> will contain two copies of <code>A</code>s member variables: one via <code>B</code>, and one via <code>C</code>. These will be accessible independently, using scope resolution.

Instead, if classes <code>B</code> and <code>C</code> inherit virtually from class <code>A</code>, then objects of class <code>D</code> will contain only one set of the member variables from class <code>A</code>.

This feature is most useful for multiple inheritance, as it makes the virtual base a common subobject for the deriving class and all classes that are derived from it. This can be used to avoid the diamond problem by clarifying ambiguity over which ancestor class to use, as from the perspective of the deriving class (<code>D</code> in the example above) the virtual base (<code>A</code>) acts as though it were the direct base class of <code>D</code>, not a class derived indirectly through a base (<code>B</code> or <code>C</code>).

It is used when inheritance represents restriction of a set rather than composition of parts. In C++, a base class intended to be common throughout the hierarchy is denoted as virtual with the <code>virtual</code> keyword.

Consider the following class hierarchy.

As declared above, a call to <code>bat.eat()</code> is ambiguous because there are two <code>Animal</code> (indirect) base classes in <code>Bat</code>, so any <code>Bat</code> object has two different <code>Animal</code> base class subobjects. So, an attempt to directly bind a reference to the <code>Animal</code> subobject of a <code>Bat</code> object would fail, since the binding is inherently ambiguous:

To disambiguate, one would have to explicitly convert <code>bat</code> to either base class subobject:

In order to call <code>eat()</code>, the same disambiguation, or explicit qualification is needed: <code>static_cast<Mammal&>(bat).eat()</code> or <code>static_cast<WingedAnimal&>(bat).eat()</code> or alternatively <code>bat.Mammal::eat()</code> and <code>bat.WingedAnimal::eat()</code>. Explicit qualification not only uses an easier, uniform syntax for both pointers and objects but also allows for static dispatch, so it would arguably be the preferable method.

In this case, the double inheritance of <code>Animal</code> is probably unwanted, as we want to model that the relation (<code>Bat</code> is an <code>Animal</code>) exists only once; that a <code>Bat</code> is a <code>Mammal</code> and is a <code>WingedAnimal</code>, does not imply that it is an <code>Animal</code> twice: an <code>Animal</code> base class corresponds to a contract that <code>Bat</code> implements (the "is a" relationship above really means "implements the requirements of"), and a <code>Bat</code> only implements the <code>Animal</code> contract once. The real world meaning of "is a only once" is that <code>Bat</code> should have only one way of implementing <code>Eat</code>, not two different ways, depending on whether the <code>Mammal</code> view of the <code>Bat</code> is eating, or the <code>WingedAnimal</code> view of the <code>Bat</code>. (In the first code example we see that <code>Eat</code> is not overridden in either <code>Mammal</code> or <code>WingedAnimal</code>, so the two <code>Animal</code> subobjects will actually behave the same, but this is just a degenerate case, and that does not make a difference from the C++ point of view.)

This situation is sometimes referred to as diamond inheritance (see Diamond problem) because the inheritance diagram is in the shape of a diamond. Virtual inheritance can help to solve this problem.

The solution

We can re-declare our classes as follows:

The <code>Animal</code> portion of <code>Bat::WingedAnimal</code> is now the same <code>Animal</code> instance as the one used by <code>Bat::Mammal</code>, which is to say that a <code>Bat</code> has only one, shared, <code>Animal</code> instance in its representation and so a call to <code>Bat::Eat</code> is unambiguous. Additionally, a direct cast from <code>Bat</code> to <code>Animal</code> is also unambiguous, now that there exists only one <code>Animal</code> instance which <code>Bat</code> could be converted to.

The ability to share a single instance of the <code>Animal</code> parent between <code>Mammal</code> and <code>WingedAnimal</code> is enabled by recording the memory offset between the <code>Mammal</code> or <code>WingedAnimal</code> members and those of the base <code>Animal</code> within the derived class. However this offset can in the general case only be known at runtime, thus <code>Bat</code> must become (<code>vpointer</code>, <code>Mammal</code>, <code>vpointer</code>, <code>WingedAnimal</code>, <code>Bat</code>, <code>Animal</code>). There are two vtable pointers, one per inheritance hierarchy that virtually inherits <code>Animal</code>. In this example, one for <code>Mammal</code> and one for <code>WingedAnimal</code>. The object size has therefore increased by two pointers, but now there is only one <code>Animal</code> and no ambiguity. All objects of type <code>Bat</code> will use the same vpointers, but each <code>Bat</code> object will contain its own unique <code>Animal</code> object. If another class inherits from <code>Mammal</code>, such as <code>Squirrel</code>, then the vpointer in the <code>Mammal</code> part of <code>Squirrel</code> will generally be different to the vpointer in the <code>Mammal</code> part of <code>Bat</code> though they may happen to be the same if the <code>Squirrel</code> class is the same size as <code>Bat</code>.

Additional Example of Several Ancestors

This example to illustrates a case where base class <code>A</code> has a constructor variable <code>msg</code> and an additional ancestor <code>E</code> is derived from grandchild class <code>D</code>. <pre> A / \ B C \ / D | E </pre> Here, <code>A</code> must be constructed in both <code>D</code> and <code>E</code>. Further, inspection of the variable <code>msg</code> illustrates how class <code>A</code> becomes a direct base class of its deriving class, as opposed to a base class of any intermediate deriving classed between <code>A</code> and the final deriving class.

Pure Virtual Methods

Suppose a pure virtual method is defined in the base class. If a deriving class inherits the base class virtually, then the pure virtual method does not need to be defined in that deriving class. However, if the deriving class does not inherit the base class virtually, then all virtual methods must be defined.

References