title: Scope of a local object
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope">Can a local variable&#39;s memory be accessed outside its scope?</a><br>
    <a href="https://stackoverflow.com/questions/6689030/return-reference-to-local-variable">return reference to local variable</a>  </p>
  </blockquote>
  
  
  
  <p>This is  a sample program written for checking the scope of a local class object inside a function. Here I am creating an object of class A and assigning values to it and returns that object by reference in <code>Function()</code>. I want to know that when the scope of the variable will end?.Since it is a stack object(not pointer), Is it will be destructed in the end of the Function()? If so what will happen when its reference value is assigned to a new object?</p>
  
  <pre><code>  #include "stdafx.h"
    #include &lt;iostream&gt;
  
    class A
    {
    public:
      int a, b;
      A(int aa, int bb)
      {
        a = aa;
        b = bb;
      }
      A(){}
    };
  
    A&amp; Function()
    {
      A object;
      object.a = 10;
      object.b = 20;
      return object;
    }
  
    int _tmain(int argc, _TCHAR* argv[])
    {
      A aaa = Function();
      std::cout&lt;&lt;"\nValue : "&lt;&lt;aaa.a&lt;&lt;" "&lt;&lt;aaa.b;
      getchar();
      return 0;
    }
  </code></pre>