Virtual Inheritance is used to solve the Dreaded Diamond Problem associated with multiple inheritance in C++.
Virtual Inheritance is a kind of inheritance in which the part of the object that belongs to the virtual base class becomes a common direct base for the derived class and any other class that derives from it. In other words, if class A
is virtually derived from class V
, and class B
is derived (directly or indirectly) from A
, then V
becomes a direct base class of class B
and any other class derived from A
.
Virtual Inheritance is used to solve the problem of Dreaded Diamond Problem in C++.
When Do we use Virtual Inheritance?
Generally, virtual base classes are most suitable when the classes that derive from the virtual base, and especially the virtual base itself, are pure abstract classes. This means the classes above the "join class" have very little if any data.