Xref: utzoo comp.lang.c++:13997 comp.std.c++:953 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!paperboy!think.com!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!mcsun!ukc!edcastle!gaa From: gaa@castle.ed.ac.uk (Gerard A. Allan) Newsgroups: comp.lang.c++,comp.std.c++ Subject: Adding multiple inheritance to a single inheritance class library. Keywords: multiple inheritance, virtual base Message-ID: <10971@castle.ed.ac.uk> Date: 11 Jun 91 18:55:12 GMT Organization: Edinburgh Microfabrication Facility Lines: 55 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. 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" ? I'd be interested in hearing how programmers have dealt with this problem. Gerard A. Allan | Post: EMF gaa@castle.ed.ac.uk | Kings Buildings JANET:gaa@uk.ac.ed.castle | University of Edinburgh Internet:gaa%castle.ed.ac.uk@cunyvm.cuny.edu | Edinburgh EARN/BITNET:gaa%castle.ed.ac.uk@UKACRL | Scotland UUCP:gaa%castle.ed.ac.uk@ukc.uucp | EH9 3JL