What’s Virtual Inheritance?

Print anything with Printful



Virtual inheritance is used to complete the definition of an object by relying on a subclass to implement all of its methods. It is commonly used in multiple inheritance and is declared using the “virtual” keyword in C++. Global functions cannot be declared virtual and virtual inheritance is used to solve ambiguity resolution.

Virtual inheritance is a type of inheritance in which the implementation of a superclass is incomplete and a subclass is required for the complete definition of an object. This type of inheritance can be used in combination with single and multiple inheritance, but is most commonly used in multiple inheritance. Any class that inherits from a virtual base class becomes a direct subclass of that base class. A virtual base class can rely on a subclass to implement all of its methods, but this is not a requirement.

C++ is the computer language most commonly known to use virtual inheritance. To declare virtual inheritance in C++, the “virtual” keyword is used. Both the superclass and subclass must declare virtual methods with the “virtual” keyword. This tells the C++ compiler that the superclass is incomplete and that it needs to get information from the subclass to complete it. Using the subclass to complement the superclass doesn’t mean that subclasses override each other if they have the same base class, and instead the C++ compiler takes care of determining which pieces go with each object.

Because a virtual base class is required for virtual inheritance, global functions in C++ cannot be declared virtual. This type of inheritance can only be used when adhering to object-oriented programming (OOP) principles. The reason for this is that global functions are not associated with a particular class and therefore are generally self-contained. Without a superclass and a subclass inheritance cannot happen, so global functions and virtual inheritance are mutually exclusive. Global functions can, in theory, be used inside virtual functions, but the reverse may not always work.

Virtual inheritance is used to solve many programming problems and one of the most useful ones is ambiguity resolution. In multiple inheritance, you can have a base class A that has two subclasses, B and C, and then a class D that inherits from both classes B and C. This pattern is commonly called a “death diamond” because if the classes A, B, and C all have implementations of the same method, it’s not possible for class D to determine which implementation it should use. Virtual inheritance solves this problem because the implementation of each class remains distinct and therefore unambiguous. This distinction is handled by specialized internal objects called virtual tables (vtables) which keep track of each object type, but these tables need not be explicitly manipulated by a programmer because they are built into the language.




Protect your devices with Threat Protection by NordVPN


Skip to content