Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!apple!fernwood!lia!steve From: steve@lia (Stephen Williams) Newsgroups: comp.lang.c++ Subject: Re: Bug in multiple inheritence Message-ID: <1991Jan17.190452.5204@lia> Date: 17 Jan 91 19:04:52 GMT Reply-To: steve@lia.com (Stephen Williams) Organization: Litton/Integrated Automation, Alameda, California Lines: 51 Let me remind you of my problem. ... >The following program demonstrates an apparent bug in C++ 2.0 and 2.1. >Am I seeing things? > > >class X { int x, y; }; >class Y { int a, b; }; >class Mess : public X, public Y { int m; }; > > >main() >{ > Mess m; > Mess &n = m; > > X x = n; > Y y = n; // <---- C++ Error!!!! In this case y should get the > // Y part of n, but is instead getting the X part. > // An offset is missing. > X &xp = n; > Y &yp = n; // <---- Works. In this case the correct code > // is generated. >} ken@tucana.csis.dit.csiro.au respondes: >Isn't it unsafe anyway to assign a derived class instance to a base class >instance? I decided to check this out. I searched the Ellis-Stroustrup C++ book and found no mention either way about casting a derived INSTANCE to a base class instance, but by experiment it seems that the AT&T compiler does the assignment for single inheritence correctly, even to the point of getting virtual functions right. However, using the example above and assuming that class X had a virtual function and Mess redefined it, x.vfunc() would call X::vfunc(), whereas xp.vfunc() would call Mess::vfunc(). The problem in my test was that the compiler was inlining the virtual functions for x.vfunc(), but not for xp.vfunc(). The moral: although assignment of derived class instance to base class instance is allowed by the compiler, it is probably illegal in the language, and again I complain about the compiler. Comments? --Steve steve@lia.com