title: How do I move dynamically allocated data from an object to another?
body: <p>I have an Array class written with RAII in mind (super simplified for the purpose of this example):</p>
  
  <pre><code>struct Array
  {
      Array(int size) {
          m_size = size;
          m_data = new int[m_size];
      }
  
      ~Array() {
          delete[] m_data;
      }
  
      int* m_data = nullptr;
      int  m_size = 0;
  };
  </code></pre>
  
  <p>Then I have a function that takes a reference to an Array and does some operations on it. I use a temporary Array <code>temp</code> to perform the processing, because for several reasons I can't work directly with the reference. Once done, I would like to transfer the data from the temporary Array to the real one:</p>
  
  <pre><code>void function(Array&amp; array)
  {
      Array temp(array.m_size * 2);
  
      // do heavy processing on `temp`...
  
      array.m_size = temp.m_size;
      array.m_data = temp.m_data;
  }
  </code></pre>
  
  <p>The obvious problem is that <code>temp</code> goes out of scope at the end of the function: its destructor is triggered, which in turn deletes the memory. This way <code>array</code> would contain non-existent data. </p>
  
  <p>So what is the best way to "move" the ownership of data from an object to another?</p>