Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your object is resource-heavy, you might want to avoid copying altogether, and just provide the move constructor and move assignment operator. However, if you really want copying too, it is easy to provide all of the operations.</p> <p>Your copy operations look sensible, but your move operations don't. Firstly, though an rvalue reference parameter will <strong>bind</strong> to an rvalue, within the function it is an <strong>lvalue</strong>, so your move constructor ought to be:</p> <pre><code>AnObject( AnObject&amp;&amp; rh ) : n( std::move(rh.n) ), a( std::move(rh.a) ) { rh.n = 0; rh.a = nullptr; } </code></pre> <p>Of course, for fundamental types like you've got here it doesn't actually make a difference, but it's as well to get in the habit.</p> <p>If you provide a move-constructor, then you don't need a move-assignment operator when you define copy-assignment like you have --- because you accept the parameter by <strong>value</strong>, an rvalue will be moved into the parameter rather than copied.</p> <p>As you found, you can't use <code>std::swap()</code> on the whole object inside a move-assignment operator, since that will recurse back into the move-assignment operator. The point of the comment in the post you linked to is that you don't need to implement a custom <code>swap</code> if you provide move operations, as <code>std::swap</code> will use your move operations. Unfortunately, if you don't define a separate move assignment operator this doesn't work, and will still recurse. You can of course use <code>std::swap</code> to swap the members:</p> <pre><code>AnObject&amp; operator=(AnObject other) { std::swap(n,other.n); std::swap(a,other.a); return *this; } </code></pre> <p>Your final class is thus:</p> <pre><code>class AnObject { public: AnObject( size_t n = 0 ) : n( n ), a( new int[ n ] ) {} AnObject( const AnObject&amp; rh ) : n( rh.n ), a( new int[ rh.n ] ) { std::copy( rh.a, rh.a + n, a ); } AnObject( AnObject&amp;&amp; rh ) : n( std::move(rh.n) ), a( std::move(rh.a) ) { rh.n = 0; rh.a = nullptr; } AnObject&amp; operator = ( AnObject rh ) { std::swap(n,rh.n); std::swap(a,rh.a); return *this; } ~AnObject() { delete [] a; } private: size_t n; int* a; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload