Path: utzoo!attcan!uunet!aplcen!haven!ncifcrf!lhc!nih-csl!sparkler.dcrt.nih.gov!kgorlen From: kgorlen@sparkler.dcrt.nih.gov (Keith Gorlen) Newsgroups: comp.lang.c++ Subject: Re: Multiple Inheritance flaw Message-ID: <148@nih-csl.nih.gov> Date: 7 Jun 90 02:00:22 GMT References: <7050013@hpcupt1.HP.COM> <58170024@hpclscu.HP.COM> Sender: news@nih-csl.nih.gov Organization: National Institutes of Health, Bethesda, MD Lines: 53 In article <58170024@hpclscu.HP.COM> shankar@hpclscu.HP.COM (Shankar Unni) writes: >> 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. Or *might* be true! This is what I encountered in making the NIH Class Library, which follows a Smalltalk-80 -like tree, support MI. Even though the library classes are organized in a tree and don't use MI themselves, a user of the library *may* wish to multiply inherit from two or more library classes. To permit this, Object (the root class of the tree), must be a virtual base class, for example. From what I've done and heard about MI so far, it looks like virtual base classes are frequently needed. Jerry Schwarz, author of the AT&T iostream library (an early application of C++ MI), even remarked a while back that he thought virtual should be the *default* for base class inheritance! It's too bad it is so tricky to use. >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. Each class in the NIH Class Library implements the member function castdown() to cast an Object* to a pointer to a derived class. It uses run-time type information to exhaustively search an object's inheritance DAG to find the (unique) occurrence of the desired derived class, and calls a member function of that class which returns its "this" pointer as a void*. It would be better (more efficient) if downward casts from virtual bases were supported by C++. -- Keith Gorlen phone: (301) 496-1111 Building 12A, Room 2033 uucp: uunet!nih-csl!kgorlen National Institutes of Health Internet: kgorlen@alw.nih.gov Bethesda, MD 20892