Creating a diamond-shaped hierarchy
When using multiple inheritance, sometimes it is tempting to utilize sibling (or cousin) classes as base classes for a new derived class. When this happens, the hierarchy is no longer a tree in shape, but rather, a graph containing a diamond.
Whenever an object of the derived class type is instantiated in such a situation, two copies of the common base class will be present in the instance of the derived class. Duplication of this sort obviously wastes space. Additional time is also wasted by calling duplicate constructors and destructors for this repeated subobject and by maintaining two parallel copies of a subobject (most likely unnecessarily). Ambiguities also result when trying to access members from this common base class.
Let’s see an example detailing this issue, starting with abbreviated (and simplified) class definitions of LifeForm
, Horse
, and Person
. Though only portions of the full program example are shown, the program...