WEBVTT

00:00.300 --> 00:05.970
Hello again! In this video, we are going to have an overview of the copy constructor. The copy constructor

00:05.970 --> 00:07.650
is a special member function.

00:07.920 --> 00:10.590
It is actually a specialized version of the constructor.

00:11.310 --> 00:17.430
The constructor gets called when we create a new object, and it can be passed any number and type of

00:17.430 --> 00:17.910
arguments.

00:18.480 --> 00:19.440
It can even be passed

00:19.440 --> 00:22.560
no arguments at all, in which case it is the default constructor.

00:23.700 --> 00:29.160
The copy constructor, on the other hand, can only take one argument, which is an object of the same

00:29.160 --> 00:29.550
class.

00:30.810 --> 00:37.260
So when the new object is created, it will be initialized to have the same values as the other object.

00:37.680 --> 00:40.530
This other object is passed as an argument by reference.

00:40.980 --> 00:46.740
So if we have a Test class, it will take a Test object by reference, and that should usually be a

00:46.740 --> 00:49.500
reference to const, as that is more efficient.

00:49.800 --> 00:53.610
And also, it means you cannot accidentally change the thing that you are copying from.

00:56.800 --> 01:02.710
If we initialize a new object from an existing object, the copy constructor will automatically be called.

01:03.550 --> 01:09.730
So if we have some test1 object which we create and we call the constructor with arguments x and

01:09.730 --> 01:16.120
y, for example. Then if we have a new test2 object, which is initialized from test1, that

01:16.120 --> 01:21.430
will call the copy constructor, and the data in test2 will have the same values as the data members in

01:21.430 --> 01:21.970
test1.

01:23.890 --> 01:26.230
Something that looks a bit confusing at first.

01:26.920 --> 01:31.780
If you have an equals sign in here, but you are creating a new object, that will also call the copy

01:31.810 --> 01:35.210
constructor, because that is not an assignment of an existing object.

01:35.620 --> 01:37.750
It is creating a completely new object.

01:39.490 --> 01:43.510
So test3 will be a new object whose members have the same values as test1.

01:48.200 --> 01:52.010
The main situation where the copy constructor is used is in function calls.

01:52.610 --> 01:59.390
If we pass an argument by value, the called function will have a new object created on its stack, and

01:59.390 --> 02:01.160
that will be a copy of the argument.

02:01.580 --> 02:07.000
So the copy constructor will be invoked there, to make the new object a copy of the argument.

02:08.150 --> 02:11.180
Also see when we return a local variable by value.

02:11.690 --> 02:17.630
The function has a return space where it stores the return value, and the new object will be created

02:17.630 --> 02:21.530
in that return space. Which is a copy of the variable that is being returned.

02:23.360 --> 02:27.950
However, the copy constructor does not get called every time we pass or return something by value.

02:28.340 --> 02:32.960
There are some optimizations that compilers are allowed to do, which will reduce the amount of copying.

02:33.380 --> 02:35.840
And we will look at those later on, in a different video.

02:36.740 --> 02:39.680
So the copy constructor may not always be called when you think it should be.

02:42.020 --> 02:43.880
In C++, there is always an exception!

02:47.270 --> 02:52.700
If we do not actually write a copy constructor for the class, the compiler will generate one for

02:52.700 --> 02:58.280
us. That is called a synthesized copy constructor and this will just call the copy constructor for all

02:58.280 --> 02:58.790
the members.

02:59.300 --> 03:03.200
If you have a member which is an object, it will call the copy constructor for that class.

03:03.830 --> 03:06.620
If you have a member which is a built-in type, it will copy it.

03:07.430 --> 03:11.210
And for the large majority of classes, that will be good enough.

03:12.050 --> 03:16.430
There are occasionally situations where that does not give the right behaviour, and then you need

03:16.430 --> 03:18.350
to write your own copy constructor.

03:19.370 --> 03:23.720
The usual case is when you have a class that manages a resource of some kind. So that could be

03:23.720 --> 03:27.830
allocated memory, a thread object, a database connection.

03:28.190 --> 03:33.770
If you just do a naive copy, then both objects end up owning the same resource, and they are going to

03:33.770 --> 03:34.760
conflict with each other.

03:36.290 --> 03:41.300
So you want to make sure that each object has its own version of the resource. And to do that, the

03:41.300 --> 03:47.570
new object has to acquire the resource for itself, and then initialize it from the object that it is copying.

03:50.530 --> 03:52.780
So we will see an example of that later on.

03:53.170 --> 03:59.920
But for the synthesized copy constructor, if we have a class Test which has two members, an int and

03:59.920 --> 04:04.280
a string, then the compiler will generate a copy constructor, which looks like this.

04:04.540 --> 04:06.790
You cannot actually see it in the code, but it will be there.

04:07.600 --> 04:10.000
So this will be the copy constructor for the class Test.

04:10.420 --> 04:12.850
It will take some Test object by reference to const.

04:13.540 --> 04:19.600
It will initialize the "i" member from the argument's "i" member and the string from the argument's string.

04:21.400 --> 04:25.600
One of these things we are going to do throughout this course is to build up our own string

04:25.600 --> 04:30.970
class. It will be String with a capital "S", so it does not get mixed up with the library string class.

04:31.480 --> 04:38.110
And we will build up this string class as we learn more C++ features. To start off with, this is going to be

04:38.110 --> 04:41.110
very simple, because we do not really know all that many features.

04:41.530 --> 04:47.020
We're going to use a library string as a member and then delegate everything to that.

04:49.170 --> 04:54.180
We are going to have a constructor which will take a library string and initialize our member from

04:54.180 --> 04:56.130
that. The copy constructor

04:56.160 --> 05:01.950
will take an object of the same type by reference. As opposed to the constructor, which took an

05:01.950 --> 05:03.180
object of a different type.

05:04.170 --> 05:10.300
(Different spelling. C++ is case sensitive!) And then in the copy constructor,

05:10.320 --> 05:17.370
we are going to initialize our string member from the string member of the argument. And then we have

05:17.370 --> 05:20.250
a function which is going to print out the data in our string member.

05:22.410 --> 05:26.250
In our main function, we are going to create some objects of our class.

05:26.760 --> 05:30.120
So we are going to use a string literal to initialize it.

05:30.780 --> 05:32.100
Then we call the copy constructor.

05:32.730 --> 05:35.800
Then we call it again, using the syntax with an equals sign.

05:36.300 --> 05:38.400
And then we check that we have the data that we expect.

05:39.300 --> 05:41.100
So let's try this out.

05:45.020 --> 05:45.950
So there we are.

05:45.980 --> 05:52.250
We initialized a string with a library string, "world" and we can print out "world", so that works.

05:53.420 --> 05:57.500
We have initialized our string with the same data, in two different ways.

05:58.640 --> 06:03.680
OK, so that is just the beginning of our string class, with a capital "S". It will get more interesting,

06:03.680 --> 06:04.190
I promise!

06:04.670 --> 06:06.470
But that is all for this video.

06:06.890 --> 06:07.730
I will see you next time.

06:07.730 --> 06:09.620
But meanwhile, keep coding!
