Path: utzoo!attcan!uunet!cs.utexas.edu!samsung!zaphod.mps.ohio-state.edu!sdd.hp.com!hplabs!hpda!hpcupt1!thomasw From: thomasw@hpcupt1.HP.COM (Thomas Wang) Newsgroups: comp.lang.c++ Subject: Re: Multiple Inheritance flaw Message-ID: <7050018@hpcupt1.HP.COM> Date: 7 Jun 90 20:01:07 GMT References: <7050013@hpcupt1.HP.COM> Organization: Hewlett Packard, Cupertino Lines: 48 / hpcupt1:comp.lang.c++ / shankar@hpclscu.HP.COM (Shankar Unni) / 2:46 pm Jun 4, 1990 / > There is a serious flaw in C++'s multiple inheritance scheme. The manual > reads "One can cast from a derived class to a virtual base class, but not > from a virtual base class to a derived class." > > Normally, one would make a list class that operates on the base class. > With the run-time type information, it is safe to cast to a derived class. > This no longer works with multiple inheritance. The base class is usually > declared 'public virtual'. So even when it is obviously safe to type cast > upward, C++ still cannot do it. I don't know about base classes being "usually" public virtual. Multiple inheritance is a very powerful and unwieldy tool. Virtual inheritance is the most tricky part of the whole setup to use - it should be used only in those cases where a base class defines some unique property for an object as a whole, and can be derived along *more than one path*. Remember the last part: you should not be using "virtual" (mainly for efficiency considerations) unless the last condition is true. The reason why you cannot go back from a virtual base class to the derived class is mainly the above point: you could have got to the base class element along more than one path. Even if, in your heirarchy, there is only one way to get from the base to the derived class, you still cannot do this safely. Besides, there is always the implementation issue: Since, when using virtual base classes, the derived class never has an exact image of any intermediate class (between the base class and this derived class), this operation my be impossible. Consider: class A {}; class B : public virtual A {}; class C : public virtual A {}; class D : public B, public C {}; Given an object of class D, there is no single contiguous chunk of it that matches the layout of a C object. ONE LAST POINT: Remember that you can always use explicit casts, but you will get really unexpected results in the above case... ----- Shankar Unni E-Mail: Hewlett-Packard California Language Lab. Internet: shankar@hpda.hp.com Phone : (408) 447-5797 UUCP: ...!hplabs!hpda!shankar ----------