Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!tut.cis.ohio-state.edu!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: VIRTUAL-NESS LOST - A C++ mystery involving funcs returning objects not ptrs Keywords: virtual C++ overloaded operator interpreter Message-ID: <10588@alice.UUCP> Date: 17 Mar 90 14:20:32 GMT References: <33629@genrad.UUCP> <33897@genrad.UUCP> <2887@ohm.sw.mcc.com> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 60 In article <2887@ohm.sw.mcc.com>, creemer@ohm.sw.mcc.com (David Zachary Creemer) writes: > Yes, but, the AT&T 2.0 language reference manual (the one I get with > my Sun CC compiler) reads, > "The interpretation of the call of a virtual function depends on the > type of an object for which it was called,..." Did my earlier posting on this subject get lost? This example is a bug in cfront 2.0. It's fixed in cfront 2.1. The statement in the manual is correct. HOWEVER, if you have a function void f(Base b) { /* stuff that deals with b */ } then calling f with a derived object: Derived d; f(d); is a request to use the Base(const Base&) constructor to form a copy of the Base part of d and use that as the argument to f. Once inside f, the formal parameter is a Base, not a Derived. If you had said void f(Base& b) { /* stuff that deals with b */ } the situation would be quite different. > It makes no mention of the pointer-to-object vs. object argument going > on here. So, as I read it, cfront is behaving exactly as the Language > Ref. manual says it should., Furthermore, given the following: > class Base { > public: > virtual void print() { cout << "base\n"; } > }; > class Der : public Base { > public: > virtual void print() { cout << "der\n"; } > }; > Base& func() { Der *x = new Der; return *x; } > main() { func().print(); } > both g++ (1.37.something) and CC call the derived class' print method. > I argue that this approach is best. References are supposed to give > "pointer like" behavior (call by reference) without having to be > bothered by pointer dereferencing problems. In any event, it's a > curious fact which will undoubtedly cause someone a headache at some > time. Both g++ and CC are correct this time. -- --Andrew Koenig ark@europa.att.com