Newsgroups: comp.lang.c++ Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!decwrl!fernwood!lia!jgro From: jgro@lia (Jeremy Grodberg) Subject: Re: polymorphism on base objects? Message-ID: <1991May14.204538.2326@lia> Reply-To: jgro@lia.com (Jeremy Grodberg) References: Distribution: comp Date: Tue, 14 May 91 20:45:38 GMT In article dpg@extro.ucc.su.OZ.AU (D P Gilbert) writes: >I was experimenting with polymorphism to see if it works through a >function that takes a reference to a base class and got the following >surprises. 2 compilers (SUN C++ and Comeau on ix/386) based on cfront >[2.0 gave the same results.] > [ example omitted ] >These last 2 results seem to indicate I can get polymorphism via >base objects? Apologies if this is a stupid question. This is not a stupid question, it is a bug in cfront 2.0. Under certain circumstances (the details of which I do not know), the default copy constructor incorrectly copies over the vtable pointer, giving the new object the wrong virtual functions. I belive this bug does not show up if the object being copied is a different size from the object being copied to, which is why it does not cause a lot of problems in real code (since derived classes almost always add at least one member). The results you expected were in fact the correct results, and the compiler was wrong. -- Jeremy Grodberg "Show me a new widget that's bug-free, and I'll show jgro@lia.com you something that's been through several releases."