Returning Large Objects in Functions
<p>Compare the following two pieces of code, the first using a reference to a large object, and the second has the large object as the return value. The emphasis on a "large object" refers to the fact that repeated copies of the object, unnecessarily, is wasted cycles.</p>

<p>Using a reference to a large object:</p>

<pre><code>void getObjData( LargeObj&amp; a )
{
  a.reset() ;
  a.fillWithData() ;
}

int main()
{
  LargeObj a ;
  getObjData( a ) ;
}
</code></pre>

<p>Using the large object as a return value:</p>

<pre><code>LargeObj getObjData()
{
  LargeObj a ;
  a.fillWithData() ;
  return a ;
}

int main()
{
  LargeObj a = getObjData() ;
}
</code></pre>

<p>The first snippet of code does not require copying the large object.</p>

<p>In the second snippet, the object is created inside the function, and so in general, a copy is needed when returning the object.  In this case, however, in <code>main()</code> the object is being declared. Will the compiler first create a default-constructed object, then copy the object returned by <code>getObjData()</code>, or will it be as efficient as the first snippet?</p>

<p>I think the second snippet is easier to read but I am afraid it is less efficient.</p>

<p><strong>Edit:</strong> Typically, I am thinking of cases <code>LargeObj</code> to be generic container classes that, for the sake of argument, contains thousands of objects inside of them. For example,</p>

<pre><code>typedef std::vector&lt;HugeObj&gt; LargeObj ;
</code></pre>

<p>so directly modifying/adding methods to <code>LargeObj</code> isn't a directly accessible solution.</p>
