Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!uakari.primate.wisc.edu!crdgw1!ge-dab.ge.com!tarpit!tous!alfred!fang!att!westmark!mole-end!mat From: mat@mole-end.UUCP (Mark A Terribile) Newsgroups: comp.lang.c++ Subject: Re: Bug in multiple inheritence Summary: It's legal, actually Message-ID: <472@mole-end.UUCP> Date: 20 Jan 91 20:53:02 GMT References: <1991Jan17.190452.5204@lia> Organization: mole-end--private system. admin: mole-end!newtnews Lines: 63 > >The following program demonstrates an apparent bug in C++ 2.0 and 2.1. > >class Mess : public X, public Y { int m; }; > > 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. > 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 there's no cast. In fact, that's legal. In section 5.4, page 69, we read (in commentary, I grant) that if it is legal to convert Mess* to Y* then it is legal to convert Mess to Y& , and even to do so by cast: void f( X ); void f( Y ); Mess m; f( (Y&) m ); // Unambiguous: it will call f( Y ) . ... 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(). What you have is a genuine bug. > The problem in my test was that the compiler was inlining the virtual > functions for x.vfunc(), but not for xp.vfunc(). A definite bug, actually. The compiler is known to assume in dealing with references that it is dealing with an exact type; it is not. > 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? Well, yes. It's quite legal. See section 12.8 in the ARM, p 297 in the hardcover. It is made quite clear: ``This implies ($12.3) that objects of a derived class can be assigned to objects of a public base class. ...'' It is also noted in the commentary that this `slicing' may seem dangerous, but that it is consistant with all the other rules of the language, and that protections against probably would give a false confidence. -- (This man's opinions are his own.) From mole-end Mark Terribile