Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
When the question of whether to use multiple inheritance
comes up, ask at least two questions:
1. Do you need to show the public interfaces of both these classes
through your new type? (See instead if one class can be contained within the
other, with only some of its interface exposed in the new class.)
2. Do you need to upcast to both of the base classes? (This also
applies when you have more than two base classes.)
If you can answer no to either question, you can avoid
using MI and should probably do so.
Watch for the situation where one class needs to be upcast
only as a function argument. In that case, the class can be embedded and an
automatic type conversion function provided in your new class to produce a
reference to the embedded object. Any time you use an object of your new class
as an argument to a function that expects the embedded object, the type
conversion function is used. However,
type conversion can t be used for normal polymorphic member function selection;
that requires inheritance. Preferring composition over inheritance is a good
overall design guideline.
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |