title: Copy constructor and operator= for a dynamic array inside a class
body: <p>I need some help to implement a copy constructor and a operator= for my dynamic array inside my class. <strong>Im not allowed to use a vector at this point</strong> </p>
  
  <p>Here is mye code:
  Class:</p>
  
  <pre><code>    class Matrix{
      private:
          int rows;
          int columns;
          double* matrix;
      public:
          Matrix();
          explicit Matrix(int N);
          Matrix(int M, int N);
          void setValue(int M, int N, double value);
          double getValue(int M, int N);
          bool isValid() const;
          int getRows();
          int getColumns();
          ~Matrix();
          friend ostream&amp; operator&lt;&lt;(ostream &amp;out, Matrix&amp;matrix1);       
  };
  </code></pre>
  
  <p>And my code:</p>
  
  <pre><code>Matrix::Matrix(){
      matrix = NULL;
  }
  
  Matrix::Matrix(int N){
      matrix = new double[N * N];
      rows = N;
      columns = N;
  
      for(int i = 0; i &lt; N; i++){
          for(int j = 0; j &lt; N; j++){
              if(i==j)
                  matrix[i * N + j] = 1;
              else
                  matrix[i * N + j] = 0;
          }
      }
  }
  
  Matrix::Matrix(int M, int N){
      matrix = new double[M * N];
      rows = M;
      columns = N;
  
      for(int i = 0; i &lt; M; i++){
          for(int j = 0; j &lt; N; j++)
              matrix[i * N + j] =  0;
      }
  }
  
  Matrix::~Matrix(){
      delete [] matrix;
  }
  
  void Matrix::setValue(int M, int N, double value){
      matrix[M * columns + N] = value;
  }
  
  double Matrix::getValue(int M, int N){
      return matrix[M * columns + N];
  }
  
  bool Matrix::isValid() const{
      if(matrix==NULL)
          return false;
      else
          return true;
  }
  
  int Matrix::getRows(){
      return rows;
  }
  
  int Matrix::getColumns(){
      return columns;
  }
  
  ostream&amp; operator&lt;&lt;(ostream &amp;out, Matrix&amp;matrix1){
      if(matrix1.isValid())
          for(int i = 0; i &lt; matrix1.getRows(); i++){
              for(int j = 0; j &lt; matrix1.getColumns(); j++)
                  out &lt;&lt; matrix1.getValue(i,j) &lt;&lt; "\t";
              out &lt;&lt; endl;
          }
      else
          out &lt;&lt; "Matrisen er ikke gyldig."; //the matrix is not valid
      return out;
  }
  </code></pre>
  
  <p>Would something like this work?</p>
  
  <pre><code>Matrix &amp;operator=(const Matrix &amp;m) {*(this-&gt;matrix) = *(m.matrix);}
  Matrix(const Matrix &amp;rhs) : matrix(0) {
          this-&gt;matrix = new double();
          *(this-&gt;matrix) = *(rhs.matrix);
      }
  </code></pre>
  
  <p>Im also going to overload the operator += and +.
  I've tried to implement this as:</p>
  
  <pre><code>const Matrix operator+=(Matrix matrix1, Matrix matrix2){
          if(!matrix1.isValid() || !matrix2.isValid()){
              cout &lt;&lt; "En av matrisene er ugyldig"; //one of the matrices are invalid
              return Matrix::Matrix(); //returning a NULL matrix
          }
          else{
              if(matrix1.getColumns()==matrix2.getColumns() &amp;&amp; matrix1.getRows()==matrix2.getRows()){
                  Matrix temp(matrix1.getRows(), matrix1.getColumns());
                  for(int i = 0; i &lt; matrix1.getRows(); i++){
                      for(int j = 0; j &lt; matrix1.getColumns(); j++)
                          temp.setValue(i,j,(matrix1.getValue(i,j) + matrix2.getValue(i,j)));
                  }
                  return temp;
              }
              else{
                  cout &lt;&lt; "Matrisene har ikke samme dimensjon"; //the matrices does not have the same dimensions
                  return Matrix::Matrix();
              }
          }
      }
      const Matrix operator+(Matrix matrix1, Matrix matrix2){
          return matrix1 += matrix2;
      }
  </code></pre>
  
  <p>Would something like this work? Is it better to overload the operators as a part of the class, a friend of the class or just outside the class?</p>
  
  <p>Any help is appriciated.</p>