Rvalue references in C ++

   // move constructor
    ArrayWrapper (ArrayWrapper&& other)
        : _p_vals( other._p_vals  )
        , _size( other._size )
    {
        other._p_vals = NULL;
        other._size = 0;
    }

I found a tutorial on rvalue links. I really do not understand why we should establish other._p_vals = NULL;and the other._size = 0; Author explains:

But why do we need to set other._p_vals = NULL? The reason is that destructor - when a temporary object goes beyond the scope, like all other C ++ objects, its destructor will work.

If it goes out of scope and is destroyed, why install other._p_vals = NULL

When its destructor is running, it will free _p_vals. The same _p_vals that we just copied!

I thought we did not copy, or ... am I mistaken?

other._p_vals NULL, move - , . : , , !

- ?!?

+4
1

, , , :

ArrayWrapper::~ArrayWrapper() {
    delete[] _p_vals;
}

"" other. other ( "" , _size = 0), , other , , , .

- .

+4

Source: https://habr.com/ru/post/1665966/


All Articles