title: How to correctly specify the copy constructor
body: <p>Assume I have a class Matrix, with a constructor as follows:</p>
  
  <pre><code>Matrix::Matrix(int rows, int cols)
  {
      nrows = a; //here nrows is the number of rows for each matrix 
      ncols = b; //here ncols is the number of cols for each matrix
      p = new double [rows*cols];
      for(int i=0;i&lt;rows*cols;i++) 
          { 
              *p++ = 0.0;
          }
  }
  </code></pre>
  
  <p>Suppose I also have a 'copy' constructor as follows:</p>
  
  <pre><code>Matrix::Matrix(const Matrix&amp; mat)
  {   p = new double[mat.nrows*mat.ncols];
      for(int i=0;i&lt;mat.nrows*mat.ncols;i++)
          { 
               p[i] = mat.p[i];
  
          }
  }
  </code></pre>
  
  <p>Now also suppose I have the following lines in my main function: </p>
  
  <pre><code>int main()
      {
          Matrix A(2,2);
          Matrix B(2,2);
          A = Matrix(B); //call overloaded assignment operator, and copy ctor/
       }
  </code></pre>
  
  <p>Here the '=' operator is overloaded to assign all the elements in B to A.
  My issue is that once the call to the copy constructor is made, the Matrix A object is a completely new object. </p>
  
  <p>Is there a better way of writing the copy constructor so that if Matrix A already exists then calling A = Matrix(B) results in an error?</p>