title: Object returned from function and copy constructor
body: <p>There is such code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class A {
  public:
      int a;
      A() : a(0) {
          std::cout &lt;&lt; "Default constructor" &lt;&lt; " " &lt;&lt; this &lt;&lt; std::endl;
      }
      A(int a_) : a(a_) {
          std::cout &lt;&lt; "Constructor with param " &lt;&lt; a_ &lt;&lt; " " &lt;&lt; this &lt;&lt; std::endl;
      }
      A(const A&amp; b) {
          a = b.a;
          std::cout &lt;&lt; "Copy constructor " &lt;&lt; b.a &lt;&lt; " to " &lt;&lt; a &lt;&lt; " " &lt;&lt; &amp;b &lt;&lt; " -&gt; " &lt;&lt; this &lt;&lt; std::endl;
      }
      A&amp; operator=(const A&amp; b) {
          a=b.a;
          std::cout &lt;&lt; "Assignment operator " &lt;&lt; b.a &lt;&lt; " to " &lt;&lt; a &lt;&lt; " " &lt;&lt; &amp;b &lt;&lt; " -&gt; " &lt;&lt; this &lt;&lt;  std::endl;
      }
      ~A() {
          std::cout &lt;&lt; "Destructor for " &lt;&lt; a &lt;&lt; " " &lt;&lt; this &lt;&lt; std::endl;
      }
      void show(){
        std::cout &lt;&lt; "This is: " &lt;&lt; this &lt;&lt; std::endl;
      }
  };
  
  
  A fun(){
    A temp(3);
    temp.show();
    return temp;
  }
  
  
  int main() {
      {
        A ob = fun();
        ob.show();
      }
      return 0;
  }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>Constructor with param 3 0xbfee79dc
  This is: 0xbfee79dc
  This is: 0xbfee79dc
  Destructor for 3 0xbfee79dc
  </code></pre>
  
  <p>Object ob is initialized by function fun(). Why copy constructor is not called there? I thought that when function returns by value then copy constructor or assignment operator is called. It seems that object constructed in function fun() is not destroyed after execution of function. How can be copy constructor forced to invoke in this case?</p>
  
  <p>This was compiled by g++.</p>