title: Why does returning a reference to a automatic variable work?
body: <p>I'm currently reading about <em>C++</em>, and I read that when using return by reference I should make sure that I'm  not returning a reference to a variable that will go out of scope when the function returns.</p>
  
  <p>So why in the <code>Add</code> function the object <code>cen</code> is returned by reference and the code works correctly?!  </p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Cents
  {
   private:
   int m_nCents;
  
   public:
   Cents(int nCents) { m_nCents = nCents; }
  
  int GetCents() { return m_nCents; }
  };
  
  Cents&amp; Add(Cents &amp;c1, Cents &amp;c2)
  {
     Cents cen(c1.GetCents() + c2.GetCents());
     return cen;
  }
  
  int main()
  {
     Cents cCents1(3);
     Cents cCents2(9);
     cout &lt;&lt; "I have " &lt;&lt; Add(cCents1, cCents2).GetCents() &lt;&lt; " cents." &lt;&lt; std::endl;
  
     return 0;
  }
  </code></pre>
  
  <p>I am using CodeBlocks IDE over Win7.</p>