[C++] returing object from functions

From: Spacen Jasset (a_at_b.com)
Date: 09/26/04


Date: Sun, 26 Sep 2004 01:12:08 +0100

Suppose I have a function

ParsedObject parse( const string &s )
{
    ParsedObject p;
    ...
    return p;
}

The function parses a string, and fills p full of useful information. p is
also quite a large object. On return then, the compiler must invoke p's copy
constructor since I've defined one ( and not taken the default )

This must surely be quite inefficient, since the copy constructor must be
called, and the compiler can't easily optimise this since my copy
constructor may do some fancy things like reference counting, data sharing
or the like.

Isn't:

parse( ParsedObject &p, const string &s ) always a better method, since no
copy is necessary?