On 3/6/23 17:22, MarioCPPP wrote:
...
> But I still cant'imagine its use.
> I have to understand what is a MOVE SEMANTIC.
>
> In my old-styled mind move IS copying (and deleting the
> original object).
Only when necessary. What the semantics of moving are is up to the
implementor of the relevant special functions.
A typical example is when an object doesn't contain the actual data, but
only a pointer to the data. A copy constructor would copy the data that
was pointed at, and then put a pointer to the copied data in the new
object. The destructor would deallocate the memory the data was stored in.
A move constructor, on the other hand, would move the pointer from the
existing object to the new one, replacing the pointer in the existing
object with a null pointer to indicate that it no longer had any data.
The data that was pointed at would remain untouched. As a result, a move
can be much faster than a copy followed by a delete. The only point in
creating a move constructor is if it can, indeed, be faster than a copy
followed by a delete.
I don't know anywhere near as much about r-value references as other
people, so I'll let them try to explain how that is connected.