Global Information Lookup Global Information

Virtual inheritance information


Diagram of diamond inheritance, a problem that virtual inheritance is trying to solve.

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

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

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 (D in the example above) the virtual base (A) acts as though it were the direct base class of D, not a class derived indirectly through a base (B or C).[1][2]

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 virtual keyword.

Consider the following class hierarchy.

UML virtual inheritance.svg

struct Animal {
    virtual ~Animal() = default;    // Explicitly show that the default class destructor will be made.
    virtual void Eat() {}
};

struct Mammal: Animal {
    virtual void Breathe() {}
};

struct WingedAnimal: Animal {
    virtual void Flap() {}
};

// A bat is a winged mammal
struct Bat: Mammal, WingedAnimal {};

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

Bat bat;
Animal& animal = bat;  // error: which Animal subobject should a Bat cast into, 
                // a Mammal::Animal or a WingedAnimal::Animal?

To disambiguate, one would have to explicitly convert bat to either base class subobject:

Bat bat;
Animal& mammal = static_cast<Mammal&>(bat); 
Animal& winged = static_cast<WingedAnimal&>(bat);

In order to call Eat, the same disambiguation, or explicit qualification is needed: static_cast<Mammal&>(bat).Eat() or static_cast<WingedAnimal&>(bat).Eat() or alternatively bat.Mammal::Eat() and bat.WingedAnimal::Eat(). 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 Animal is probably unwanted, as we want to model that the relation (Bat is an Animal) exists only once; that a Bat is a Mammal and is a WingedAnimal, does not imply that it is an Animal twice: an Animal base class corresponds to a contract that Bat implements (the "is a" relationship above really means "implements the requirements of"), and a Bat only implements the Animal contract once. The real world meaning of "is a only once" is that Bat should have only one way of implementing Eat, not two different ways, depending on whether the Mammal view of the Bat is eating, or the WingedAnimal view of the Bat. (In the first code example we see that Eat is not overridden in either Mammal or WingedAnimal, so the two Animal 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.

  1. ^ Milea, Andrei. "Solving the Diamond Problem with Virtual Inheritance". Cprogramming.com. Retrieved 2010-03-08. One of the problems that arises due to multiple inheritance is the diamond problem. A classical illustration of this is given by Bjarne Stroustrup (the creator of C++) in the following example:
  2. ^ McArdell, Ralph (2004-02-14). "C++/What is virtual inheritance?". All Experts. Archived from the original on 2010-01-10. Retrieved 2010-03-08. This is something you find may be required if you are using multiple inheritance. In that case it is possible for a class to be derived from other classes which have the same base class. In such cases, without virtual inheritance, your objects will contain more than one subobject of the base type the base classes share. Whether this is what is the required effect depends on the circumstances. If it is not then you can use virtual inheritance by specifying virtual base classes for those base types for which a whole object should only contain one such base class subobject.

and 17 Related for: Virtual inheritance information

Request time (Page generated in 1.182 seconds.)

Virtual inheritance

Last Update:

Virtual inheritance is a C++ technique that ensures only one copy of a base class's member variables are inherited by grandchild derived classes. Without...

Word Count : 1649

Composition over inheritance

Last Update:

large amount of repetitive code. C++ uses virtual inheritance to solve the diamond problem of multiple inheritance. The C++ examples in this section demonstrate...

Word Count : 1939

Multiple inheritance

Last Update:

in various ways, including using virtual inheritance. Alternate methods of object composition not based on inheritance such as mixins and traits have also...

Word Count : 2457

Virtual method table

Last Update:

three classes in an inheritance hierarchy: a superclass, Cat, and two subclasses, HouseCat and Lion. Class Cat defines a virtual function named speak...

Word Count : 1944

Virtual function

Last Update:

virtual. Abstract method Inheritance (object-oriented programming) Superclass (computer science) Virtual inheritance Virtual class Interface (object oriented...

Word Count : 1658

Virtual class

Last Update:

the virtual class Parts by the derived class Car. Inheritance (object-oriented programming) Superclass (computer science) Virtual inheritance Virtual function...

Word Count : 557

List of things described as virtual

Last Update:

following: Virtual airline Virtual appliance Virtual artifact Virtual community Virtual function Virtual inheritance Virtual intelligence Virtual machine...

Word Count : 187

Information hiding

Last Update:

interface.) Implementation inheritance Inheritance semantics Modularity (programming) Opaque data type Virtual inheritance Transparency (human–computer...

Word Count : 1496

Dominance

Last Update:

drawing, a style of graph drawing Dominance (C++), an aspect of virtual inheritance in the C++ programming language Dominance (economics), in economics...

Word Count : 352

Fragile base class

Last Update:

Implementation inheritance Inheritance semantics Software brittleness Virtual inheritance "Selective Open Recursion: A Solution to the Fragile Base Class Problem"...

Word Count : 738

Offsetof

Last Update:

or outright crash the program. This is especially the case for virtual inheritance. The following program will generate several warnings and print obviously...

Word Count : 939

Method overriding

Last Update:

Implementation inheritance Inheritance semantics Method overloading Polymorphism in object-oriented programming Template method pattern Virtual inheritance X-HTTP-Method-Override...

Word Count : 2163

Function pointer

Last Update:

size of a simple function pointer, in order to deal with virtual methods and virtual inheritance[citation needed]. In C++, in addition to the method used...

Word Count : 2212

Code reuse

Last Update:

Reusability Reuse metrics Single source of truth Software framework Virtual inheritance Frakes, W.B.; Kyo Kang (July 2005). "Software Reuse Research: Status...

Word Count : 1925

Object composition

Last Update:

Inheritance semantics Law of Demeter Object-oriented analysis and design Virtual inheritance Yaiser, Michelle. "Object-oriented programming concepts: Composition...

Word Count : 2283

Virtual tax

Last Update:

Virtual property, on the death of the owner, would be considered as if it were any other intangible property for the purpose of estate or inheritance...

Word Count : 535

PostgreSQL

Last Update:

the parent table will cause that column to appear in the child table. Inheritance can be used to implement table partitioning, using either triggers or...

Word Count : 8582

PDF Search Engine © AllGlobal.net