title: Move Constructor & Move Assignment
body: <p>I have been reading the book "The C++ programing language 4th edition" by Bjarne Stroustrup (The creator of c++) and have been learning about move constructors and move assignments. </p>
  
  <p>In the book for the class vector (see 1 for header below) he shows how to implement the move constructor (see 2 below) and says the move assignment is implemented in a similar manner but doesn't show how. I have implemented the move assignment myself (see 3 below) and everything seems to be working fine, however, I am not sure I have implemented it correctly. </p>
  
  <p>I am not getting any errors and have looked at many examples but I cannot confirm its correct for my specific class. Can someone experienced with c++ please look at my code and comment if it is correct?</p>
  
  <p><strong>EDIT: Also please see 4 for constructors and destructor.</strong></p>
  
  <p>Thank you for your time.</p>
  
  <p>P.S: Any helpful hints or modifications are welcome</p>
  
  <p>1) Class Header File:</p>
  
  <pre><code>#ifndef VECTOR_H
  #define VECTOR_H
  
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;stdexcept&gt;
  
  using namespace std;
  
  template&lt;typename T&gt;
  class Vector {
  
  public:
      // constructors
      Vector(int s);
      Vector(std::initializer_list&lt;T&gt;);
  
      // destructor
      ~Vector();
  
      // copy constructor and copy assignment
      Vector(Vector&amp;);
      Vector&lt;T&gt;&amp; operator=(Vector&amp;);
  
      // move constructor and move assignment
      Vector(Vector&amp;&amp;);
      Vector&lt;T&gt;&amp; operator=(Vector&amp;&amp;);
  
      // operators
      T&amp; operator[](int);
      const T&amp; operator[](int) const; // the second const means that this function cannot change the state of the class
                                      // we define operator[] the second time for vectors containing constant members;
      // accessors
      int getSize();
  
  
  private:
      int size;
      T* elements;
  
  };
  
  #endif /* VECTOR_H */
  </code></pre>
  
  <p>2) Move constructor (implemented in the same way as book):</p>
  
  <pre><code>// move constructor 
  template&lt;typename T&gt;
  Vector&lt;T&gt;::Vector(Vector&amp;&amp; moveme) : size{moveme.size}, elements{moveme.elements}
  {
      moveme.elements = nullptr;
      moveme.size = 0;
  }
  </code></pre>
  
  <p>3) Move assignment (not sure if correct):</p>
  
  <pre><code>// move assignment
  template&lt;typename T&gt;
  Vector&lt;T&gt;&amp; Vector&lt;T&gt;::operator=(Vector&amp;&amp; moveme) 
  {
      delete[] elements; // delete old values
      elements = moveme.elements;
      size = moveme.size;
      moveme.elements = nullptr;
      moveme.size = 0;
      return *this;
  }
  </code></pre>
  
  <p>4) Constructors and destructor:</p>
  
  <pre><code>#include &lt;array&gt;
  
  #include "Vector.h"
  
  // constructors
  template&lt;typename T&gt;
  Vector&lt;T&gt;::Vector(int s) {    
      if(s&lt;0) throw length_error{"Vector::Vector(int s)"};
      // TODO: use Negative_size{} after learning how to write custom exceptions
      this-&gt;size = s;
      this-&gt;elements = new T[s];
  }
  
  template&lt;typename T&gt;
  Vector&lt;T&gt;::Vector(std::initializer_list&lt;T&gt; list) : size(list.size()), 
          elements(new T[list.size()]) 
  {
      copy(list.begin(), list.end(), elements);
  }
  
  // destructor
  template&lt;typename T&gt;
  Vector&lt;T&gt;::~Vector()
  {
      delete[] this-&gt;elements;
  }
  </code></pre>