title: c++ empty copy assignment operator copies data
body: <p>I am sort of new to c++ and wanted to write a small matrix library which has a base class <code>Matrix</code> from which some new matrix types like <code>SparseMatrix</code>, <code>DenseMatrix</code>, <code>HashMatrix</code> etc.</p>
  <p>My base class looks like this:</p>
  <pre><code>
  class Matrix {
  
  
  protected:
      int m,n;
  
  
  public:
      
      
      Matrix(int m_p, int n_p) : m(m_p), n(n_p){
          std::cout &lt;&lt; &quot;gen constr&quot; &lt;&lt; std::endl;
      }
  
      Matrix(Matrix&amp; other) = delete;
  
      Matrix() = delete;
      
      virtual ~Matrix() {}
  
  
      int getM() const {
          return m;
      }
  
      int getN() const {
          return n;
      }
  
      virtual double&amp; get(int m, int n) = 0;
  
      virtual double  get(int m, int n) const = 0;
  
      inline double&amp; operator() (int m, int n){
          return get(m,n);
      }
  
      inline double  operator() (int m, int n) const{
          return get(m,n);
      }
  
      friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, Matrix &amp;matrix) {
  
          using std::scientific;
          using std::fixed;
  
          os &lt;&lt; std::fixed &lt;&lt; std::setprecision(2) &lt;&lt; scientific &lt;&lt; std::setfill(' ');
  
          for(int i = 1; i &lt;= matrix.getM(); i++){
              for(int j = 1; j &lt;= matrix.getN(); j++){
                  os &lt;&lt; std::setw(10) &lt;&lt; matrix.get(i,j)  &lt;&lt; &quot; &quot;;
              }
              os &lt;&lt; &quot;\n&quot;;
          }
          return os;
      }
      
      
      Matrix&amp; operator=(const Matrix&amp; other) {
  //        std::cout &lt;&lt; &quot;equality assign&quot; &lt;&lt; std::endl;
          return *this;
      }
  
  };
  </code></pre>
  <p>As you can see, I have overwritten the equality assign operator which simply returns the object and does not actually copy values.</p>
  <p>My first implementation of <code>DenseMatrix</code> is very straight forward:</p>
  <pre><code>class DenseMatrix : public Matrix{
  
  
  private:
      double* data;
  
  public:
  
   
      DenseMatrix(int mP, int nP) : Matrix(mP, nP){
          std::cout &lt;&lt; &quot;gen constr base&quot; &lt;&lt; std::endl;
          this-&gt;data = new double[mP * nP]{0};
      }
          
      
      
      
      DenseMatrix() = delete;
  
      ~DenseMatrix() {
          delete this-&gt;data ;
      }
  
      double &amp;get(int m, int n) {
          int index = m*getN()+n-(getN()+1);
          assert(index &lt; (getN() * getM()) &amp;&amp; index &gt;= 0);
          return this-&gt;data [index];
      }
  
      double get(int m, int n)const {
          int index = m*getN()+n-(getN()+1);
          assert(index &lt; (getN() * getM()) &amp;&amp; index &gt;= 0);
          return this-&gt;data [index];
      }
   
        
  };
  
  </code></pre>
  <p>Furthermore the <code>main()</code> function looks like this:</p>
  <pre><code>    DenseMatrix mat(3,3);
      for(int i = 1; i&lt;= 3; i++){
          mat(i,i) = i;
      }
      
      DenseMatrix mat2(3,3);
      mat2 = mat;
      
      
      std::cout &lt;&lt; mat &lt;&lt; std::endl;
      std::cout &lt;&lt; mat2 &lt;&lt; std::endl;
  
  
  &gt;&gt;&gt;  1.00e+00   0.00e+00   0.00e+00 
  &gt;&gt;&gt;  0.00e+00   2.00e+00   0.00e+00 
  &gt;&gt;&gt;  0.00e+00   0.00e+00   3.00e+00 
  
  &gt;&gt;&gt;  1.00e+00   0.00e+00   0.00e+00 
  &gt;&gt;&gt;  0.00e+00   2.00e+00   0.00e+00 
  &gt;&gt;&gt;  0.00e+00   0.00e+00   3.00e+00 
  
  </code></pre>
  <p>As you can see, I create two matrices first. I adjust the values for the first matrix and leave the values of the second matrix default to 0. Yet after calling the equality assign operator, the content of the second matrix changes even tho the function I implemented has basically no code which affects the matrix.</p>
  <p>I do not understand this behavior and would be very happy if someone could briefly explain what is going on here.</p>
  <p>Thank you very much for your patience and help :)</p>