Chapter 4 – Classes: Part 2
I’ve introduced the move concept in the previous post. Now let’s talk about it a bit.
Move semantics allows an object, under certain conditions, to take ownership of some other object’s external resources.
A correct move operation will:
- Assign the pointers (from source to destination)
- Copy primitive values
- Set source pointers to null
- (optional) set source primitive to default values
Of course, the question of why we should move objects, if we already have copy operations. That is because move is always faster (we simply set and clear a pointer, instead of allocating new memory and creation of new objects.
- Receives any type, as argument
- Returns an rvalue reference of that type
std::move(x) means “you can treat x as an rvalue”.
And let’s see how we can use it.