11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 26: Inheriting Multiple Inheritance 349As an example of the latter, consider a TeacherAssistant who is both aStudent and a Teacher, both of which are subclasses of Academician. If theuniversity gives its teaching assistants two IDs — a student ID and a separateteacher ID — the class TeacherAssistant will need to contain two copies ofclass Academician.Constructing the Objects of Multiple InheritanceThe rules for constructing objects need to be expanded to handle multipleinheritance. The constructors are invoked in the following order:1. First, the constructor for any virtual base classes is called in the order inwhich the classes are inherited.2. Then the constructor for all nonvirtual base classes is called in theorder in which the classes are inherited.3. Next, the constructor for all member objects is called in the order inwhich the member objects appear in the class.4. Finally, the constructor for the class itself is called.Notice that base classes are constructed in the order in which they are inheritedand not in the order in which they appear on the constructor line.Voicing a Contrary OpinionI should point out that not all object-oriented practitioners think that multipleinheritance is a good idea. In addition, many object-oriented languagesdon’t support multiple inheritance.Multiple inheritance is not an easy thing for the language to implement. Thisis mostly the compiler’s problem (or the compiler writer’s problem). But multipleinheritance adds overhead to the code when compared to single inheritance,and this overhead can become the programmer’s problem.More importantly, multiple inheritance opens the door to additional errors.First, ambiguities such as those mentioned in the earlier section “StraighteningOut Inheritance Ambiguities” pop up. Second, in the presence of multiple inheritance,casting a pointer from a subclass to a base class often involves changingthe value of the pointer in sophisticated and mysterious ways. Let me leavethe details to the language lawyers and compiler writers.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!