answersLogoWhite

0

What is virtual inheritance?

Updated: 12/17/2022
User Avatar

Wiki User

7y ago

Best Answer

Virtual inheritance is used in multiple inheritance hierarchies whenever two or more classes inherit from the same base class. For example, consider the following minimal example:

struct A {int x;};

struct B : A {};

struct C : A {};

struct D : B, C {};

Here, A is a common base class of both B and C, thus B::A and C::A are independent instances of A. However, D inherits from both B and C thus indirectly inherits both B::A and C::A. This introduces an ambiguity when implicitly referring to A from D, as in the following example:

D d; d.x = 42; // error: ambiguous

When the compiler sees this it won't know whether we are referring to B::A.x or C::A.x, thus we must be explicit when referring to A via D:

d.B::x = 42; // ok

The problem with this is that we still have two separate instances of x so unless we are extremely careful about which x we are referring to we can easily end up with two different values for x with no way of knowing which was the correct value with respect to D.

What we really need is for B and C to share the same instance of A and we achieve that through virtual inheritance:

struct A {int x;};

struct B : virtual A {};

struct C : virtual A {};

struct D : B, C {};

D d;

d.x = 42; // ok

Note that d.x can be referred to as d::A.x, d::B::A.x or d::C::A.x because they all refer to the same instance of x, thus the ambiguity is eliminated. In effect, the most-derived class in the hierarchy (D) is now a direct descendant of A rather than an indirect descendant of both B::A and C::A.

Note also that the virtual keyword has no effect on independent instances of either B or C because they then become the most-derived classes within their own hierarchy and therefore behave exactly as they would had the virtual keyword been omitted. The virtual keyword only comes into play when we derive from a class with a virtual base.

It is also possible to have a base class that is both virtual and non-virtual in the same hierarchy:

struct A {int x;};

struct B : virtual A {};

struct C : virtual A {};

struct D: A {}

struct E : B, C, D {};

Here, A is a virtual base of both B and C, as before, but not of D. Thus E::A implicitly refers to the virtual A while E::D::A must be explicitly referred to as E::D::A because it is a separate instance. Such classes are best avoided, but if we don't have access to the source code we sometimes don't have a choice in the matter.

However, if a base class holds no data it really doesn't matter whether it is declared virtual or not because a class that has no data consumes just one byte regardless of how many instances there are.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is virtual inheritance?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How is run time polymorphism accomplished?

Through inheritance and virtual functions.


Type of inheritances in c plus plus?

Single, multiple, multi-level, hierarchical and hybrid/virtual inheritance. Single inheritance applies when one class inherits from just one base class. Multiple inheritance applies when one class inherits from two or more base classes. Multi-level inheritance applies to a class that inherits from at least one base class that is itself derived from another base class. Hierarchical inheritance applies to a base class that is inherited by two or more separate derived classes. Hybrid inheritance combines multiple inheritance, multi-level inheritance and hierarchical inheritance. That is, where A is a common base class of derived classes B and C, and B and C are both base classes of derived class D. Hybrid inheritance is often used with virtual inheritance where B and C inherit from A virtually rather than directly. In these cases, the virtual base class is instantiated by the most-derived class in the hierarchy, D, and this instance is then shared by both B and C.


Inheritence and its types in object oriented programmings?

There are only two types of inheritance in object oriented programming: Single inheritance: where a class inherits directly from just one base class. Multiple inheritance: where a class inherits directly from two or more base classes. Multi-level inheritance is often thought of as being a separate type of inheritance, however inheritance relates to a derived class and those that it directly inherits from. If a base class is itself a derived class (an intermediate class), then its base class or classes are simply indirect base classes of the derivative. But in isolation, the intermediate class either uses single or multiple inheritance, even if its base class or classes are also intermediates. Virtual inheritance is also thought of as being a separate type, however virtual inheritance doesn't change the relationship between classes within the hierarchy. the only difference virtual inheritance makes is that the virtual base class or classes are constructed by the most-derived class within the current hierarchy, rather than by their most direct descendants. In this way, only one instance of each virtual base exists in the hierarchy, rather than multiple instance as would normally exist. The actual inheritance is still single or multiple, however.


How do you implement inheritance in c plus plus?

You implement inheritance by deriving a new class of object from an existing class of object. The existing class is known as the base class of the derived class.Classes declared final cannot be used as bases classes and classes without a virtual destructor (or a virtual destructor override) cannot be used as polymorphic base classes.


Why can't method override be used without inheritance?

Overriding a method means that you are replacing an existing or virtual method that has already been defined in the parent object class, so without using inheritance, there can be no existing method to override.


What is a virtual base class in C plus plus when the different methods in base and derived classes have the same name true or false?

False. A virtual base class is one that is common to two or more derived classes that are themselves base classes, and that may be combined through multiple inheritance. By declaring the common base class to be virtual in its direct derivatives, only one instance of the common base class exists in the multiple-inheritance classes.


Examples of inheritance in c plus plus?

Single-inheritance is where one class inherits directly from another class: class A {}; class B : public A {}; Here, class B inherits all the public and protected members of class A. Multiple-inheritance is where one class inherits directly from two or more classes: class A {}; class B {}; class C : public A, public B {}; Here, class C inherits all the public and protected members of both A and B. Multi-level inheritance is where one class inherits from another class that itself derived. class A {}; class B : public A {}; class C : public B {}; Here, class B inherits all the public and protected members of A while class C inherits all the public and protected members of B, including those inherited from A. Virtual inheritance applies to multi-level inheritance whereby a virtual base class becomes a direct ancestor to the most-derived class. This variation of inheritance is typically used in multiple inheritance situations where two or more intermediate classes inherit from the same base class: class A {}; class B : public virtual A {}; class C : public virtual A {}; class D : public B, public C {}; Here, classes B and C both inherit from class A. Without virtual inheritance this would mean class D would inherit two instances of A (B::A and C::A), thus creating ambiguity when referring to D::A. By employing virtual inheritance, D inherits directly from A, and both B and C inherit from D::A. In other words, B and C share the same instance of A. Another use of virtual inheritance is when you need to make a class final. class A; class B { friend class A; B() {} // private constructor }; class A : public virtual B { }; Here, class A is the final class. Class B is a helper class that has a private constructor while class A is declared a friend of class B. Class A is therefore the only class that can inherit from class B as it is the only class that can construct objects from class B. However, by inheriting class B virtually, we ensure that no other class can be derived from class A because virtual inheritance ensures that the most-derived class must be able to construct a class B object first. Currently, only class A has that privilege and must always be the most-derived class.


C plus plus programming language program for hybrid inheritance?

There's no such thing as hybrid inheritance in C++. Hybrid inheritance implies two or more different types of inheritance but there are really only two types of inheritance in C++ and they are mutually exclusive: single inheritance and multiple inheritance. A class that inherits directly from one class uses single inheritance. A class that inherits directly from two or more classes uses multiple inheritance. The only way to combine these two inheritance patterns is through multi-level inheritance, where a class inherits directly from one or more derived classes. However, whenever we create a derivative, we're only concerned with the base class or classes we are directly inheriting from. The fact they may or may not be derivatives themselves is largely irrelevant from the viewpoint of the derivative. Indeed, the only time we really need to consider one of the lower bases classes is when we need to explicitly invoke a virtual function of that particular class, as opposed to implicitly invoking the most-derived override of that function as we normally would. However, this is really no different to a derived class override invoking its direct base class method. Virtual base classes are also thought of as being a type of hybrid inheritance, however virtual base classes merely determine which class is responsible for the construction of those classes. Normally, a derived class is responsible for the construction of all its direct base classes, which must be constructed before the derived class can begin construction. In turn, those base classes are responsible for the construction for their own base classes. In this way, derived classes are automatically constructed from the ground up, base classes before derived classes, in the order declared by the derived class. For example, consider the following hierarchy: struct X {}; struct Y : X {}; struct Z : Y {}; Z inherits from Y so in order for a Z to exist we must first construct a Y. By the same token, Y inherits from X so in order for a Y to exist we must first construct an X. Thus when we initiate construction of a Z, that initiates construction of a Y which initiates construction of an X. Now consider a virtual base class: struct X {}; struct Y : virtual X {}; struct Z : Y {}; The construction sequence is exactly the same as before (X before Y before Z), the only difference is that when we now instantiate a Z, as the most-derived class in the hierarchy it becomes responsible for the construction of the virtual X. Z is also (still) responsible for the construction of a Y, but Y no longer needs to construct an X because a (virtual) X already exists. Virtual base classes become more relevant in multiple inheritance, where two or more base classes share a common base class: struct W {}; struct X : virtual W {}; struct Y : virtual W {}; struct Z : X, Y {}; Here, Z uses multiple inheritance from X and Y. Both X and Y use single inheritance from W. Without virtual inheritance, Z would inherit two separate instances of W, specifically X::W and Y::W. But by declaring W as a virtual base of X and Y, the most-derived class, Z, becomes responsible for the construction of W, as well as its direct base classes, X and Y. Neither X nor Y need to construct a W because a W will already exist. Thus X::W and Y::W now refer to the same instance of W. Note that we do not need to write any additional code for this mechanism to work. The virtual keyword alone is all we need. Even if X or Y provided explicit initialisation of W, those initialisers would be ignored by the compiler since initialisation of W is automatically the responsibility of the most-derived class. The only time those explicit initialisers would be invoked is if we explicitly instantiate an instance of X or Y, because then X or Y become the most-derived class.


Does hybrid inheritance consist of ANY two types of inheritance?

There are only two types of inheritance to begin with: single inheritance and multiple inheritance. Since they are mutually exclusive there is no such thing as hybrid inheritance.


Is a cash inheritence community property?

No. An inheritance is separate property.No. An inheritance is separate property.No. An inheritance is separate property.No. An inheritance is separate property.


Different types of inheritances?

Single Inheritance Multiple Inheritance Multilevel Inheritance


Define of inheritance?

clearly define inheritance and the reason behind using inheritance