title: Which way is preferred to store returned objects for use in a function?
body: <p>I use functions that return an object, like <code>Bar()</code> in the following example:</p>
  
  <pre><code>class Foo
  {
  public:
      Foo(int _x, int _y) : x(_x), y(_y) {}
  private:
      int x, y;
  }
  
  Foo Bar()
  {
      return Foo(1, 2);
  }
  </code></pre>
  
  <p>Not involving those with need for extra tools or lines of code (like returning a pointer or using <code>std::auto_ptr</code>), I know two ways to store the returned object in a local variable when I want to read it (call <code>const</code> methods only):</p>
  
  <pre><code>const Foo  foo1 = Bar();
  const Foo &amp;foo2 = Bar();
  </code></pre>
  
  <p>Both of them are correct in C++, the first option should have <a href="http://www.parashift.com/c++-faq-lite/return-by-value-optimization.html" rel="nofollow">no copy overhead</a>.</p>
  
  <p>Are there any differences between these two options in terms of performance? Should one of them be preferred?</p>
  
  <p><hr/>
  <strong>EDIT</strong></p>
  
  <p>OK, I’ll add a reason why I care about this. Assume the implementation of <em>Bar</em> gets changed:</p>
  
  <pre><code>Foo globalFoo(1, 2);  // This object could also be a class member.
  Foo &amp;Bar()
  {
      return globalFoo;
  }
  </code></pre>
  
  <p>My original piece of code defining <code>foo1</code> and <code>foo2</code> should still work but <code>foo2</code> gets bound to <code>globalFoo</code>, so it requires less memory<sup>1</sup>, right? Is therefore using references better since it could profit from such changes of interface?</p>
  
  <p><sup>1</sup> Assuming the usual case that an object takes more memory than a pointer.</p>