This rule basically states that if a class defines one(or more) of the following, it should probably explicitly define all three, which are:
Now let us try to understand why?
The default constructors and assignment operators do shallow copy and we create our own constructor and assignment operators when we need deep copy (For example when class contains pointers pointing to dynamically allocated resources)
First, what does a destructor do? It contains code that is supposed to run whenever an object is destroyed. To only affect the contents of the object would be useless. An object in the process of being destroyed cannot have any changes made to it. Therefore, the destructor affects the program’s state as a whole.
Now, supposing our class does not have a copy constructor. Then copying an object will copy all of its data members to the target object. What happens when these objects are destroyed? The destructor runs twice. Also the destructor has the same information available for each object being destroyed. That sums up as, in the absence of a copy constructor, the execution of the destructor, which is supposed to happen once, happens twice. This duplicate execution, is a source for trouble.
A coding example as follows:
In the example above, once the program goes out of scope, the class destructor is called, not once but twice. First due to deletion of a1, and then of a2. The default copy constructor makes a copy of the pointer vals and does not actually allocate memory for it. Thus, on deletion of a1, the destructor frees up vals. All subsequent vals containing instances when trying to be deleted by the destructor, causes the program to crash, as vals does not exist anymore.
This is similar in the case of copy assignment operator. If a class does not have an explicitly defined assignment operator, implicit assignment of all source’s data members to the target’s corresponding data members will occur. All in all, it creates a copy, which again is the same problem defined previously.
This article is contributed by Nihar Ranjan Sarkar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Vector of Vectors in C++ STL with Examples
- Array of Vectors in C++ STL
- Difference between Virtual function and Pure virtual function in C++
- cin get() in C++ with Examples
- Difference between Increment and Decrement Operators
- Map of Vectors in C++ STL with Examples
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- C++ program to print all Even and Odd numbers from 1 to N
- Reverse the content of a file and store it in another
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples