Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!usc!apple!snorkelwacker!bloom-beacon!hstbme.mit.edu!ahodgson From: ahodgson@hstbme.mit.edu (Antony Hodgson) Newsgroups: comp.std.c++ Subject: Unnecessary copying of returned object Message-ID: <1990Aug31.235606.166@athena.mit.edu> Date: 31 Aug 90 23:56:06 GMT References: <1990Aug31.234937.29938@athena.mit.edu> Sender: daemon@athena.mit.edu (Mr Background) Reply-To: ahodgson@hstbme.mit.edu.UUCP (Antony Hodgson) Organization: Massachusetts Institute of Technology Lines: 22 I'm a fairly recent user of C++, but I've already noticed one situation in which a lot of unnecessary object creation occurs which I think could be solved by a slight change in syntax. This is the common situation in which one creates a temporary object which is intended to be the returned object, but since it is a local object, one must return a copy of it. Two objects are therefore created when only one ought to be needed. Consider the following: Vector Vector::operator + ( Vector & A ) { Vector temp; // for all valid i, temp[i] = (*this)[i] + A[i] return temp; // copy of temp made here } // temp deallocated here Could an intelligent compiler bypass this copying without the help of any new syntax? Or, failing that, would it be reasonable to introduce a new keyword such as 'returnvalue' which would be defined to have the same type as the specified return value? Any comments? Tony Hodgson ahodgson@hstbme.mit.edu