Category: 

What is Virtual Inheritance?

Article Details
  • Written By: Jessica Susan Reuter
  • Edited By: Allegra J. Lingo
  • Last Modified Date: 03 December 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
Snake charmers get snakes to “dance” because of the movement of their flute-like instruments, not their music.  more...

December 4 ,  1945 :  The United States Senate approved of US participation in the United Nations.  more...

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.

Ad

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.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email