Hello again!
In this video, we are going to look at special member functions in C++11.
We now have these two new special functions, the move constructor and the move assignment operator.
These are optimized versions of the copy constructor and the assignment operator, which move their
argument instead of copying it.
And they are automatically called when the object being copied or assigned from is a moveable rvalue.
These have some implications on the rules for special member functions.
So we are going to look at those.
The compiler will not synthesize a move
operator, if the class has a copy constructor, assignment operator or destructor.
It will only define a move operator if none of those are defined, and also every data member of the class
is either a built-in type, a user-defined type which has move operators.
So something which is moveable. Or else a static data member, which is not moved, because a static data member
member is shared between all the objects of a class.
The move constructor which is synthesised by the compiler will call the move constructor for each
member of the class, and the same for the move assignment operator.
If your class defines a move operator, then booth the copy operators will be synthesized by the compiler
as deleted.
So that means you cannot copy or assign to objects of your class.
If you want to be able to do that, then you must provide your own copy operators.
Otherwise, the class will be move-only.
So the easiest way to create a move only class is to implement the move operators and not the copy
operators.
We have talked about the rule of three for when you need to implement special member functions.
And, with these two new special member functions, we now have the rule of five in C++11.
If your class needs a destructor to function correctly, then probably it needs the copy operators and
the move operators as well.
For example, if your class allocates memory in the constructor by calling new, then it needs
to have a destructor which calls delete.
It needs to have a copy constructor and a copy assignment operator to perform deep copies. And move operators
to make sure that the argument is correctly invalidated.
So let's look at an example.
I have written a very simple class, which does not actually have any data members.
We implement the move operators, which are just really shells. The compiler will synthesize copy operators,
but they will be declared as deleted.
So this is what the code will look like, as the compiler sees it.
If we called the move operators, then these will work.
So we get the move constructor called and the move assignment operator called. If we try to use the copy
constructor,
then we get this error again: "calling deleted functions".
And if I uncomment this code.
So this is what the compiler actually thinks the code does.
Then we get the same error again.
So it is as if the copy operators were declared as deleted.
So with the code as it is, you cannot copy or assign to objects of your class.
If you want to do that, then you would have to implement the copy operators to do this.
Okay, so that is it for this video.
I will see you next time.
But until then, keep coding!