Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!sdd.hp.com!ucsd!dog.ee.lbl.gov!ux5.lbl.gov!beard From: beard@ux5.lbl.gov (Patrick C Beard) Newsgroups: comp.lang.c++ Subject: Re: multiple inheritance bug Keywords: multiple inheritance, virtual base classes Message-ID: <9592@dog.ee.lbl.gov> Date: 6 Feb 91 04:54:01 GMT References: <3553@island.COM> Reply-To: beard@ux5.lbl.gov (Patrick C Beard) Organization: Berkeley Systems, Inc. Lines: 47 X-Local-Date: Tue, 5 Feb 91 20:54:01 PST In article <3553@island.COM> lars@island.COM (Lars Nyman) writes: # #I have a problem with multiple inheritance using virtual base classes, and #suspect it's a bug in the C++ compiler. Don't be so quick to call this a bug. It's actually a feature. C++ does not officially support the calling of virtual functions from a constructor. The reason? If you are in the base class constructor of a derived class, and you call a virtual function, if it called a function that is defined in the derived class, that function would be passed a pointer to an incompletely constructed derived object. # #Three level hierarchy: Base, Derived, MostDerived. # #class Base { ... } #class Derived : virtual Base { ... } #class MostDerived : Derived, Mixin { ... } # #The problem occurs when executing inside Derived's constructor. If #Derived() calls a function in Base, which in turns calls a virtual #function implemented in Derived, the 'this' pointer will be incorrect #(see below code for more technical info). #Basically, makes it impossible to use multiple inheritance... No it doesn't. It just makes it impossible to have a constructor call virtual functions of classes that are farther down in the inheritance tree. This shouldn't pose much of a problem. My philosophy is that constructors are for initialization. They should perform no work. That is deferred until the object is completely initialized. I find that this makes my designs much more robust. # #I have tested it under Sun C++ 2.0 and MPW C++ 3.1, both which are #based on CFront 2.0. # #Is this fixed in CFront 2.1 ? #Can somebody who have a C++ implementation based on CFront > 2.0 check #whether the same problem exist ? I would predict that this "feature" will remain in C++ always. -- ------------------------------------------------------------------ | Patrick C. Beard, Software Engineer, Berkeley Systems, Inc. | | "Heroes of technology." | | beard@lbl.gov, d0346@applelink.apple.com (ATTN: Patrick) |