Utilizing virtual base classes to eliminate duplication
We have just seen the MI implementation issues that quickly arise when a diamond shape is included in an OO design – duplication in memory for a repeated subobject, ambiguity accessing that subobject (even through inherited member functions), and the duplication of construction and destruction. For these reasons, pure OO designs will not include graphs in a hierarchy (that is, no diamond shapes). Yet, we know C++ is a powerhouse of a language and anything is possible. As such, C++ will provide us with a solution to these issues.
Virtual base classes are a C++ language feature that can alleviate duplication of a common base class when using multiple inheritance. The keyword virtual
is placed in the base class list between the access label and the base class name of the sibling or cousin class that may later be used as a base class for the same derived class. Note that knowing two sibling classes may later be combined...