Multiple inheritance
In C++, a class can be derived from several base classes. Going back to our birds, let’s make an observation—while flying birds have a lot in common with each other, they also have something in common with other flying animals, specifically, the ability to fly. Since flight isn’t limited to birds, we may want to move the data and the algorithms related to processing flight into a separate base class. But there’s also no denying that an eagle is a bird. We could express this relation if we used two base classes to construct the Eagle
class:
class Eagle : public Bird, public FlyingAnimal { ... };
In this case, the inheritance from both base classes is public, which means that the derived class inherits both interfaces and must fulfill two separate contracts. What happens if both interfaces define a method with the same name? If this method isn’t virtual, then an attempt to invoke it on the derived class is ambiguous, and the program doesn’t compile. If the method is virtual and the derived class has an override for it, then there’s no ambiguity since the method of the derived class is called. Also, Eagle
is now both Bird
and FlyingAnimal
:
Eagle* e = new Eagle; Bird* b = e; FlyingAnimal* f = e;
Both conversions from the derived class into the base class pointer are allowed. The reverse conversions must be made explicitly using a static or a dynamic cast. There’s another interesting conversion—if we have a pointer to a FlyingAnimal
class that’s also a Bird
class, can we cast from one to the other? Yes, we can with a dynamic cast:
Bird* b = new Eagle; // Also a FlyingAnimal FlyingAnimal* f = dynamic_cast<FlyingAnimal*>(b);
When used in this context, the dynamic cast is sometimes called a cross-cast—we aren’t casting up or down the hierarchy (between derived and based classes) but across the hierarchy—between the classes on different branches of the hierarchy tree.
Cross-cast is also mostly responsible for the high runtime cost of the dynamic cast we have seen in the previous section. While the most common use of dynamic_cast
is to cast from Base*
to Derived*
to verify that a given object is really of the derived class, the cast could also be used to cast between bases of the same derived class. This is a much harder problem. If you just want to check that the base class object is really a derived one, the compiler knows the Derived
type at this point (you cannot use the dynamic cast on incomplete types).
Therefore, the compiler knows exactly what base classes this derived type has and can trivially check if yours is one of them. But when casting across the hierarchy, the compiler knows only two base classes: at the time when this code was written, a derived class that combines both may not exist, it will be written later. But the compiler must generate the correct code now. So, the compiler has to generate code that, at run time, digs through all the possible classes that are derived from both base classes to see if yours is one of them (the actual implementation is less straightforward and more efficient than that, but the task to be accomplished remains the same).
In reality, this overhead is often unnecessary because, most of the time, the dynamic cast is indeed used to find out whether the base class pointer really points to a derived object. In many cases, the overhead is not significant. But if better performance is required, there is no way to make the dynamic cast faster. If you want a fast way to check whether a polymorphic object is really of a given type, you have to use virtual functions and, unfortunately, a list of all possible types (or at least all the ones you might be interested in):
enum type_t { typeBase, typeDerived1, typeDerived2 }; class Base { virtual type_t type() const { return typeBase; } }; class Derived1 : public Base { type_t type() const override { return typeDerived1; } }; … void process_derived1(Derived1* p); void do_work(Base* p) { if (p->type() == typeDerived1) { process_derived1(static_cast<Derived1*>(p)); } }
Multiple inheritance is often maligned and disfavored in C++. Much of this advice is outdated and stems from the time when compilers implemented multiple inheritance poorly and inefficiently. Today, with modern compilers, this isn’t a concern. It’s often said that multiple inheritance makes the class hierarchy harder to understand and reason about. Perhaps it would be more accurate to say that it’s harder to design a good multiple inheritance hierarchy that accurately reflects the relations between different properties, and that a poorly designed hierarchy is difficult to understand and reason about.
These concerns mostly apply to hierarchies that use public inheritance. Multiple inheritance can be private as well. There’s even less reason to use multiple private inheritance instead of composition than there was to use single private inheritance. However, the empty base optimization can be done on multiple empty base classes and remains a valid reason to use private inheritance, if it applies:
class Empty1 {}; class Empty2 {}; class Derived : private Empty1, private Empty2 { int i; }; // sizeof(Derived) == 4 class Composed { int i; Empty1 e1; Empty2 e2; }; // sizeof(Composed) == 8
Multiple inheritance can be particularly effective when the derived class represents a system that combines several unrelated, non-overlapping attributes. We’ll encounter such cases throughout this book when we explore various design patterns and their C++ representations.