WEBVTT

00:00.150 --> 00:00.720
Hello again!

00:01.020 --> 00:05.340
In this video, we are going to look at special member functions in C++11.

00:06.810 --> 00:13.620
We now have these two new special functions, the move constructor and the move assignment operator.

00:14.310 --> 00:19.290
These are optimized versions of the copy constructor and the assignment operator, which move their

00:19.290 --> 00:21.300
argument instead of copying it.

00:22.470 --> 00:28.110
And they are automatically called when the object being copied or assigned from is a moveable rvalue.

00:30.790 --> 00:34.870
These have some implications on the rules for special member functions.

00:34.870 --> 00:36.220
So we are going to look at those.

00:37.270 --> 00:39.820
The compiler will not synthesize a move

00:39.820 --> 00:45.460
operator, if the class has a copy constructor, assignment operator or destructor.

00:47.110 --> 00:52.840
It will only define a move operator if none of those are defined, and also every data member of the class

00:52.840 --> 00:57.820
is either a built-in type, a user-defined type which has move operators.

00:58.450 --> 01:05.890
So something which is moveable. Or else a static data member, which is not moved, because a static data member

01:05.890 --> 01:08.800
member is shared between all the objects of a class.

01:11.340 --> 01:16.860
The move constructor which is synthesised by the compiler will call the move constructor for each

01:17.190 --> 01:21.180
member of the class, and the same for the move assignment operator.

01:25.180 --> 01:31.510
If your class defines a move operator, then booth the copy operators will be synthesized by the compiler

01:31.510 --> 01:32.380
as deleted.

01:33.130 --> 01:37.510
So that means you cannot copy or assign to objects of your class.

01:38.110 --> 01:42.250
If you want to be able to do that, then you must provide your own copy operators.

01:42.820 --> 01:44.890
Otherwise, the class will be move-only.

01:45.610 --> 01:52.180
So the easiest way to create a move only class is to implement the move operators and not the copy

01:52.180 --> 01:52.840
operators.

01:56.400 --> 02:01.890
We have talked about the rule of three for when you need to implement special member functions.

02:02.580 --> 02:08.400
And, with these two new special member functions, we now have the rule of five in C++11.

02:09.450 --> 02:15.330
If your class needs a destructor to function correctly, then probably it needs the copy operators and

02:15.330 --> 02:16.950
the move operators as well.

02:20.330 --> 02:25.850
For example, if your class allocates memory in the constructor by calling new, then it needs

02:25.850 --> 02:27.980
to have a destructor which calls delete.

02:28.520 --> 02:35.660
It needs to have a copy constructor and a copy assignment operator to perform deep copies. And move operators

02:35.660 --> 02:38.420
to make sure that the argument is correctly invalidated.

02:43.790 --> 02:45.440
So let's look at an example.

02:46.010 --> 02:49.910
I have written a very simple class, which does not actually have any data members.

02:51.080 --> 02:59.900
We implement the move operators, which are just really shells. The compiler will synthesize copy operators,

02:59.900 --> 03:02.420
but they will be declared as deleted.

03:03.230 --> 03:06.770
So this is what the code will look like, as the compiler sees it.

03:09.140 --> 03:12.590
If we called the move operators, then these will work.

03:15.250 --> 03:21.730
So we get the move constructor called and the move assignment operator called. If we try to use the copy

03:21.730 --> 03:22.330
constructor,

03:25.070 --> 03:28.490
then we get this error again: "calling deleted functions".

03:33.790 --> 03:35.800
And if I uncomment this code.

03:35.890 --> 03:38.830
So this is what the compiler actually thinks the code does.

03:40.300 --> 03:41.710
Then we get the same error again.

03:42.250 --> 03:46.000
So it is as if the copy operators were declared as deleted.

03:46.810 --> 03:51.280
So with the code as it is, you cannot copy or assign to objects of your class.

03:51.700 --> 03:55.990
If you want to do that, then you would have to implement the copy operators to do this.

03:57.100 --> 03:58.510
Okay, so that is it for this video.

03:58.900 --> 03:59.740
I will see you next time.

03:59.740 --> 04:01.930
But until then, keep coding!
