title: C++ Copy constructor & Operators
body: <p>I'm currently preparing for my exam in C++, and today i came  across the following problem:</p>
  
  <p>Given the following program:</p>
  
  <pre><code>#include &lt;iostream&gt; 
  using namespace std;
  class A
  {
  private:
      int x, y;
  public:
      A(int X = 0, int Y = 0) :x(X), y(Y) 
      {
          cout &lt;&lt; "A Constructor" &lt;&lt; endl;
      }
      A(A&amp; a) 
      {
          x = a.x; y = a.y;  
          cout &lt;&lt; "A Copy Constructor" &lt;&lt; endl;  
      }
      A operator+(A a) 
      {
          A tmp(*this);  
          tmp.x += a.x;
          tmp.y += a.y;
          return tmp;
      }
      A&amp; operator=(A&amp; a)
      { 
           x = a.x;
           y = a.y;   
           return *this; 
      }
  };
  int main()
  {
      A a(1, 2), b(3, 4), c; 
      c = a + b; 
      system("pause");
      return 0; 
  } 
  </code></pre>
  
  <p>Which of the following will be the output of the program?</p>
  
  <p><strong>1)</strong> </p>
  
  <p>A Constructor<br>
  A Constructor<br>
  A Constructor<br>
  A Copy Constructor<br>
  A Copy Constructor<br>
  A Copy Constructor </p>
  
  <p><strong>2)</strong></p>
  
  <p>A Constructor<br>
  A Constructor<br>
  A Constructor<br>
  A Copy Constructor<br>
  A Copy Constructor<br>
  A Copy Constructor<br>
  A Copy Constructor</p>
  
  <p><strong>3)</strong></p>
  
  <p>A Constructor<br>
  A Constructor<br>
  A Constructor<br>
  A Copy Constructor                                                                 </p>
  
  <p><strong>4)</strong></p>
  
  <p>A Constructor<br>
  A Constructor<br>
  A Copy Constructor<br>
  A Copy Constructor<br>
  A Copy Constructor </p>
  
  <p>This is a question from the past year exam, and the problem is, that when i run the above code in Visual C++ 2017, it gives an error like you can see in the picture:</p>
  
  <p><a href="https://i.stack.imgur.com/G7QzK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G7QzK.png" alt="enter image description here"></a></p>
  
  <p>And it says: <strong>no operator "=" matches these operands, operand types are: A = A</strong>.</p>
  
  <p>I realized what's wrong in the code, and found that the <strong>operator+</strong> Method should be like this:</p>
  
  <pre><code>A&amp; operator+(A&amp; a)
  </code></pre>
  
  <p>Instead of</p>
  
  <pre><code>A operator+(A a)  
  </code></pre>
  
  <p>But the semester is over, and i have no one to ask, so i come here.
  I don't know exactly which version of VC++ was used in the past year exam,
  and because an option of error is not included in the answer options, i understand (maybe) that this code was valid in an earlier version of C++. but my questions are:</p>
  
  <p>Do this error comes from the differences between the versions of C++? </p>
  
  <p>What is the major difference between the above two versions of <strong>operator+</strong> method?</p>
  
  <p>Are the <strong>operator+</strong> and <strong>operator=</strong> methods must have the same return type (i mean by reference or by value)?</p>
  
  <p>Is the parameter of the <strong>operator+</strong> and <strong>operator=</strong> methods must be the same like the parameter of the Copy Constructor?</p>
  
  <p>Any help will be appreciated! Thanks!!</p>