title: How to control the copy of return object into a new object?
body: <p>In this code <code>operator =</code> is overloaded to control operator behaviour for class <code>A</code> objects, and copy constructor was also declared to see if it was used for the copying process.</p>
  
  <p>My question is, what does the compiler use to copy the return object of <code>a_object + b_object</code> into <code>c_object</code> if copy constructor and assignment operator was not used as shown in the output? And how to control the behaviuor of copying in this case?</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  
  #define PUTLINE(X)  std::cout&lt;&lt;X&lt;&lt;"----------------------------\n" ;
  
  class A{
    int value ;
  public:
    A(int v = 0) : value(v) {}
  
    A(const A &amp; a) 
    { 
      std::cout &lt;&lt; "copy constructor...\n" ; 
      value = a.value ;
    }
    const A &amp; operator = (const A &amp; a) {
      std::cout &lt;&lt; " = operator... \n" ;
      this-&gt;value = a.value ;    
      return *this ;
    }
  
    const A operator + (const A &amp; a) {
      return A(value + a.value) ;
    } 
  
    void print(std::ostream &amp; os) {
      os &lt;&lt; value  &lt;&lt; std::endl ;
    }
  };
  
  
  int main () {
      A a_object(10); 
      A b_object(20);
      PUTLINE(1);
      A c_object = a_object + b_object ; // what does the compiler use to make this copy? 
      PUTLINE(2);
      A d_object = c_object;
      PUTLINE(3);
      c_object.print(std::cout);
      PUTLINE(4);
      d_object.print(std::cout);
      PUTLINE(5);
      a_object = c_object ;
      PUTLINE(6);
      a_object.print(std::cout);
  }
  </code></pre>
  
  <p>Output looked like this:</p>
  
  <pre><code>1----------------------------
  2----------------------------
  copy constructor...
  3----------------------------
  30
  4----------------------------
  30
  5----------------------------
   = operator... 
  6----------------------------
  30
  </code></pre>