Path: utzoo!attcan!uunet!zephyr.ens.tek.com!uw-beaver!mit-eddie!mintaka!think!samsung!cs.utexas.edu!milano!ohm!creemer From: creemer@ohm.sw.mcc.com (David Zachary Creemer) Newsgroups: comp.lang.c++ Subject: Re: VIRTUAL-NESS LOST - A C++ mystery involving funcs returning objects not ptrs Summary: yes, but... Keywords: virtual C++ overloaded operator interpreter Message-ID: <2887@ohm.sw.mcc.com> Date: 16 Mar 90 20:50:38 GMT References: <33629@genrad.UUCP> <33897@genrad.UUCP> Organization: MCC Software Technology Program Lines: 73 In article <33897@genrad.UUCP>, charlie@genrad.com (Charlie D. Havener) writes: > In article <33629@genrad.UUCP> you write: > > SYNOPSIS: If you return a derived class object from a > > function, can you then invoke a virtual function on it and > > expect C++ to find the right one? > > > > Class Base { virtual print(); } > > Class Der : public Base { virtual print(); } > > > > Base func() { Der x; return x; } > > > > main() { func().print(); } // should it use the Base or Der print? > > // cfront uses Der print, Zortech 2.06 uses Base print > > if it was > > main() { Base t; t = func(); t.print(); } // Clearly it should use the Base print > > > >I can find no definitive answer in any of my books or the > >C++ reference manual. Section 12.2 in the Ref manual about Temporary > >Objects comes close. It seems to say this might be implementation > >dependent. > > cfront is wrong, Zortech is right. func() returns a Base, not a pointer to ... > > Virtualness is lost in GNU C++ as well. I believe that GNU and > Zortech are right, and that cfront is wrong. > > Michael > -------------------------------------------------- > > From mit-eddie!mcc.com!mit-eddie!@MCC.COM:vaughan%cadillac.cad.mcc.com Mon Mar 12 12:12:49 1990 ... another opinion > -- > Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639 > Box 200195, Austin, TX 78720 | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan > > ------------------------------------------------------- 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,..." 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. -- David David Creemer | MCC Software Technology Program | creemer@mcc.com 512 338-3403 | 9390 Research, Kaleido II Bldg., Austin, Texas 78759