We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What is Virtual Inheritance?

By Jessica Susan Reuter
Updated: May 16, 2024

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.

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Discussion Comments
Share
https://www.easytechjunkie.com/what-is-virtual-inheritance.htm
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.