title: Correctly overload assignment operator for classes with pointer members
body: <p><strong>Correct me if I'm wrong:</strong></p>
  
  <p>I understand that when having a class with members that are pointers, a copy of a class object will result in that the pointers representing the same memory address. This can result in changes done to one class object to affect all copies of this object.</p>
  
  <p>A solution to this can be to overload the <strong>=</strong> operator. Given the example below, with an attempt to create a dynamic array class, why does making changes to <strong>MyArray1</strong> change <strong>MyArray2</strong>:</p>
  
  <p><strong>Array Class:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  
  class Array
  {
  public:
      Array(int N){               //constructor sets size of array
           size = N;
           arr = new int[N];
      }
  
      ~Array();
  
      int size;    //array elements
      int *arr;    //dynamic array pointer
  
      //fill array with random values between 1 and 100
      void fillArray() {
           for (size_t i = 0; i &lt; size; i++)
                {arr[i] = std::rand()%100;}
      }
  
      //print out array to console
      void printArray() {
           for (size_t i = 0; i &lt; size; i++)
               { std::cout &lt;&lt; arr[i] &lt;&lt; " ";}
               std::cout &lt;&lt; std::endl;
      }
  
      //overload = operator
      Array &amp;operator=(Array arr2) {
          std::swap(size, arr2.size);
          std::swap(arr, arr2.arr);
          return *this;
      }
  };
  </code></pre>
  
  <p><strong>Main.cpp:</strong></p>
  
  <pre><code> #include "Array.h"
   #include &lt;iostream&gt;
  
  int main(){
      Array MyArray1(8), MyArray2(8);
  
      MyArray1.fillArray();
      MyArray2 = MyArray1;
  
      std::cout &lt;&lt; "Print out arrays:" &lt;&lt; std::endl;
      std::cout &lt;&lt; "MyArray1: "; MyArray1.printArray();
      std::cout &lt;&lt; "MyArray2: "; MyArray2.printArray();
      std::cout &lt;&lt; std::endl;
  
      MyArray1.arr[5] = 1000;
      std::cout &lt;&lt; "MyArray2: "; MyArray2.printArray();
  
      MyArray1.fillArray();
      std::cout &lt;&lt; "MyArray2: "; MyArray2.printArray();    
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Example output:</strong></p>
  
  <pre><code>Print out arrays:
  MyArray1: 41 67 34 0 69 24 78 58
  MyArray2: 41 67 34 0 69 24 78 58
  
  MyArray2: 41 67 34 0 69 1000 78 58
  MyArray2: 62 64 5 45 81 27 61 91
  </code></pre>
  
  <p>As seen above, changes made to MyArray1, changes MyArray2. I assume the overloading of <strong>=</strong> is wrong, but how would I write it correctly?</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>Thanks to Chris Dodd in the comments, I realized it's just to implement a copy constructor like this in my class:</p>
  
  <pre><code>Array(const Array &amp;arr2){
          size = arr2.size;
          arr = new int[size];
  
          for (size_t i = 0; i &lt; size; i++)
          {
              arr[i] = arr2.arr[i];
          }
  }
  </code></pre>