• 1 year ago
Copy constructor and assignment provide functions for copying objects. Move constructor and assignment move data of one object to another. https://www.softprayog.in/programming/copy-and-move-semantics-for-objects-in-cpp

Category

🤖
Tech
Transcript
00:00Hello and welcome to this video. I am Karnesh Jauri and this video is about copy and move in C++.
00:10Objects of basic types like int, char, double are routinely copied.
00:17Even for concrete classes, if their objects comprise of data members which are of basic fundamental types,
00:26the copy semantics is member-wise copy. Each data member is copied from the source object to the destination object.
00:36But when we have a source object of slightly sophisticated type like vector, it is a little different.
00:44A vector is a pointer to an array of some type like double.
00:50So member-wise copy is not correct. We need to allocate memory for the elements and set the pointer to the start of the array in the vector object.
01:02When we design the class, we need to think how objects would be copied.
01:08And we need to add a copy constructor and copy assignment operator to the class.
01:14Then there are issues of runtime efficiency.
01:18What happens when we want to copy a large object, say a vector of 5000 doubles, there are processor and memory overheads.
01:30When we want a copy and we do not need the source object after the copy, we can simply move the data from the source to the destination.
01:40This leads to move constructor and move assignment operator in the class.
01:45Let's look at copy and move in a little detail.
01:49Consider a class vector which is vector of doubles.
01:53It has a constructor which creates a vector from an initializer list of doubles.
01:59It has a destructor and the subscript operator overload function for finding an element of vector given the index and the size function which gives the size of the vector.
02:12So it is a very simple vector class and we create a vector of 6 elements 2.01, 4.99, 6.908, 9.007, 0.001 and 67.697.
02:31Next we say v2 equal to v1.
02:35We don't have any specialized code for copy constructor.
02:39So the default copy constructor is used which is member wise copy.
02:44So the element pointer in v2 is copied from v1 and both pointers point to the same array.
02:52Now we change v1 3 from 9.007 to 2.73 and we look at v2 3 which is also changed from 9.007 to 2.73.
03:07This is an error.
03:08The program ends and the destructor for v1 is called.
03:12It deletes the element array and the destructor for v2 is called which tries to delete element array.
03:20But there is no element array there to delete.
03:23So we get errors and the program aborts with the core dump.
03:28So the default member wise copy gives it leads to errors in this case and we need to make changes.
03:36We need to add a copy constructor to the vector class so that a new object which is copy of the past object reference is correctly created.
03:48A reference to vector a is passed as parameter to the constructor.
03:53In the member initializer list a new array of doubles of size a.sg is created.
04:01Also sc is initialized with a.sg and after that the elements of vector a are copied into the elements of the object being created.
04:13Similarly we have the copy assignment operator overload function.
04:17We create an array of doubles of size a.sg and copy the elements of vector a in it.
04:25We delete the old elements and make the pointer element point to the new array.
04:30We set sc to a.sg and we return a reference to the object.
04:36And now getting back to the previous program.
04:39We have added the copy constructor and the copy assignment operator.
04:44And now when we say v2 equal to v1 the copy constructor is called which creates vector v2 which is a copy of vector v1.
04:54Now v1 and v2 have separate copies of elements.
04:58So we can safely make v1 3 as 2.73 and v2 3 is not affected.
05:05It is still 9.007 and after this vector v3 is created and we say v3 equal to v1.
05:14Here copy assignment operator function is called which copies v1 into v3 and we can check the values of v3 by printing the elements.
05:25Copy constructor and copy assignment operators can be resource intensive.
05:31Copying takes time.
05:33But if the source is not required anymore after this operation.
05:38Instead of copying we can simply move the data from the source to the destination and save the time for copy.
05:46We have the move constructor and move assignment operator functions for our vector container class.
05:55Let us first look at the move constructor.
05:58The notation vector ampersand ampersand a means the reference to the r value of vector a.
06:06For any name there is the l value and r value.
06:10The l value comes on the left side of assignment operator and the r value comes from the right side of assignment operator.
06:19So the name is the l value and the data associated with the name is the r value.
06:25Here we are interested in the data and vector ampersand ampersand a gives a reference to the r value.
06:33And move constructor is quite simple.
06:36The most important part is the member initializer list where the element pointer is copied from a.element and sg is copied from a.sg.
06:47So we get the data or the data is moved.
06:50Now vector a doesn't have a valid data anymore.
06:53So a.element is made null pointer and a.sg is made 0.
07:00And our object is created which is a copy of vector a.
07:04We have saved the time that would have been spent copying the element array.
07:09And then we have the move assignment operator overload function.
07:13So that move can be used in case of assignment operator.
07:18Here again input parameters to the function is a reference to the r value of vector.
07:25We delete the old element array.
07:27And the important part highlighted is we take over the element array of vector a and copy the size a.sg to sg.
07:38As a part of housekeeping we make a.element null pointer and a.sg is 0.
07:45So there is no data left in vector a and it cannot be used anymore.
07:50We return a reference to the object.
07:52How does the compiler decide which constructor or assignment operator is to be used?
07:58There are two distinct use cases.
08:00A constructor is used when we create an object from a class.
08:05And assignment operator is actually assignment overload operator function.
08:11And is used when the assignment symbol is used in an expression.
08:16By default the copy constructor and copy assignment operators are used.
08:22In case of copy there is no loss of data and these are kind of safer.
08:27In case of move data is moved from the source to the destination resulting in loss of data at the source.
08:36So compiler generally cannot decide whether a variable won't be required later on.
08:43But in one case there is no ambiguity.
08:46Consider the example which is plus operator overload function to add two vectors u and v.
08:55In the last statement return w returns the vector w by value.
09:02So here vector w which can be quite big is to be copied to the scope of caller.
09:09So that the caller can use the result of addition of vectors u and v.
09:14But after the return statement w is no more required.
09:19So here vector w instead of copy can be moved.
09:23And if the move assignment operator is there in the class the compiler uses it by default.
09:30The compiler generally cannot decide whether to use move or not.
09:35But we can specifically write std double colon move l value which converts l value into the reference to associated r value.
09:47And the concerned move constructor or move assignment operator is used.
09:52For example we can say vector v3 braces std double colon move v0 and the move constructor is used.
10:03And in the case of vector v5 braces v4 copy constructor is used.
10:10Here we have the example program class vector.
10:14It has two data members double pointer element and size ts z.
10:21And the public member functions.
10:23The constructors.
10:25The default constructor.
10:27It creates a vector of size n with elements initialized to 0.
10:32Then a constructor to create a vector using initializer list.
10:36Next copy constructor and copy assignment operators.
10:41And then we have move constructor and move assignment operator.
10:46Next the destructor.
10:48Operator subscript overload function for getting the element at given index.
10:53And the size function which gives the size of the vector.
10:57The operator plus overload function.
11:00Adds two vectors u and v.
11:02And gives the result as vector w which is an example of move assignment.
11:07Since w is not required after the function.
11:11Its value is moved to the calling function.
11:15And the main function.
11:17We create vector v0 for use later.
11:20And the vectors v1 and v2 both having 5000 double elements.
11:25And we give some random values to the elements of v1 and v2.
11:30Next v0 equal to v1 plus v2.
11:33This is a user operator plus overload function.
11:36And an example of move assignment.
11:39And then an example of move constructor.
11:41We say v3 versus std double colon move v0.
11:47So we get a reference to the r value of v0.
11:50Which is used for installation of v3.
11:53Since it is a move constructor.
11:55Value of v0 is moved to v3.
11:57Next we create v4 and the statement v4 equal to v1.
12:03This is an example of default copy assignment.
12:06And lastly we have v5 braces v4.
12:09Which is an example of copy constructor.
12:12V5 is created and v4 is copied into v5.
12:16We can compile and run this program.
12:19It works fine.
12:20It prints a lot of diagnostic messages.
12:23Which confirm what we have coded in the example program.
12:27We have seen the copy and move semantics for objects in C++.
12:33You can find all this information at softpryok website.
12:37Here is a QR code and the link is there in the description.
12:42Please share this video and subscribe to softpryok channel.
12:47Click on the bell icon and enable notifications.
12:51Thanks very much for watching.
12:54Take care and stay safe.