Virtual inheritance is a type of inheritance in which the implementation of a superclass is incomplete, and a subclass is required for complete definition of an object. This type of inheritance can be used in conjunction with both 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 may rely on a subclass to implement all its methods, but this is not a requirement.
C++ is the most commonly known computer language 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 it must get information from the subclass to complete it. Using the subclass to complete the superclass does not mean that subclasses overwrite each other if they have the same base class, and instead the C++ compiler takes care of determining what pieces go with each object.
Since a virtual base class is required for virtual inheritance, global functions in C++ cannot be declared virtual. This inheritance type 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 so are usually self-contained on their own. Without a superclass and 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 is resolution of ambiguity. In multiple inheritance, one may 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 the "diamond of death" because if classes A, B, and C all have implementations of the same method, it isn't possible for class D to determine which implementation it should use. Virtual inheritance solves this problem because each class's implementation remains distinct, and therefore unambiguous. This distinction is handled by specialized internal objects called virtual tables (vtables) that keep track of each object type, but these tables do not need to be explicitly manipulated by a programmer because they are built into the language.