Xref: utzoo comp.lang.c++:14077 comp.std.c++:955 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!mcsun!news.funet.fi!tukki.jyu.fi!sakkinen From: sakkinen@jyu.fi (Markku Sakkinen) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Re: Adding multiple inheritance to a single inheritance class library. Keywords: multiple inheritance, virtual base Message-ID: <1991Jun14.113736.3795@jyu.fi> Date: 14 Jun 91 11:37:36 GMT References: <10971@castle.ed.ac.uk> Organization: University of Jyvaskyla, Finland Lines: 87 (Arrrgh: I already wrote and submitted something like this yesterday, but some testing of the news system here caused all articles to get lost.) In article <10971@castle.ed.ac.uk> gaa@castle.ed.ac.uk (Gerard A. Allan) writes: > >Many C++ libraries have a common root class and as a consequence they >contain many functions and methods that take the root class as an >argument requiring a cast to the desired type inside the function. >This can cause a problem when attempting to use multiple inheritance and >a virtual base class. >eg. > class Root { > virtual void function( Root *r)=0; > }; > class Derived : public Root { > int x; > virtual void function( Root *r); > }; > void Derived::function( Root *r) > { > Derived *n=(Derived *)r; > n->x++; > } > >if I now define Derived as, > class Derived : virtual public Root { > int x; > virtual void function( Root *r); > }; > >In preparation to make, > class mine : virtual public Root {}; > class multi : public mine, public Derived {}; > >The function Derived::function(Root *r) no longer works as there is an >error "cannot cast up from virtual baseclass Root" > >This is of course perfectly true (ARM 10.6c) "Casting form a virtual >base class to a derived class is disallowed to avoid requiring an >implementation to maintain pointers to enclosing objects". This >makes it very difficult to use multiple inheritance and virtual bases >from a library with a single root class. Have you realised how utterly dangerous your 'Derived::function' is in the first place? From one viewpoint it is a blessing that virtual base classes prevent such casts, which can cause any amount of havoc. There is no assurance that actual arguments will in fact refer to Derived objects, unless 'function' is redefined in no other class in your whole programme. More object-oriented languages than C++ (Simula, Eiffel, ...) typically enforce run-time checking of such casts to make them safe; in C++ such checking is not even possible. It was a deliberate (and in my opinion unfortunate) desing decision in C++ that all objects do not carry sufficient type information like they do in those others languages. Your problem is aggravated by the fact that it is hard to think of any sensible example of multiple public inheritance with _non-virtual_ base classes. Exceptions, of course, are those base classes that are not inherited over more than one path by any non-immediate descendant. >Is there some way round this problem so that I can combine classes in a >"natural" way ? After all, the cast is not ambiguous since there is only >one Derived. What solutions to this problem have C++ programmers >developed or is multiple inheritance incompatible with the single >inheritance methodology ? And finally, is there a case for requiring >"an implementation to maintain pointers to enclosing objects" ? With type information in each object, there would be no need for the restriction. Eiffel seems to have no sweat with situations like this. In C++ there is hardly any other solution than to hand-code an additional level of object-orientation and be sure to use it in all appropriate classes. I suppose some of the large general-purpose C++ class libraries have been built that way. ---------------------------------------------------------------------- "All similarities with real persons and events are purely accidental." official disclaimer of news agency New China Markku Sakkinen (sakkinen@jytko.jyu.fi) SAKKINEN@FINJYU.bitnet (alternative network address) Department of Computer Science and Information Systems University of Jyvaskyla (a's with umlauts) PL 35 SF-40351 Jyvaskyla (umlauts again) Finland ----------------------------------------------------------------------