Rule Of Three in C++

16 Jul, 2021
This rule basically states that if a class defines one (or more) of the following, it should explicitly define all three, which are:
destructor
copy constructor
copy assignment operator
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 to perform a deep copy (For example when a class contains pointers pointing to dynamically allocated resources).
First, what does a destructor do? It contains code that runs whenever an object is destroyed. Only affecting 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, suppose our class does not have a copy constructor. Copying an object will copy all of its data members to the target object. In this case when the object is destroyed the destructor runs twice. Also the destructor has the same information for each object being destroyed. In the absence of an appropriately defined copy constructor, the destructor is executed twice when it should only execute once. This duplicate execution is a source for trouble.
A coding example follows: 
C++
// In the below C++ code, we have created// a destructor, but no copy constructor// and no copy assignment operator.class Array{private:    int size;    int* vals;   public:    ~Array();    Array( int s, int* v );};  Array::~Array(){   delete vals;   vals = NULL;}  Array::Array( int s, int* v ){    size = s;    vals = new int[ size ];    std::copy( v, v + size, vals );}  int main(){   int vals[ 4 ] = { 11, 22, 33, 44 };   Array a1( 4, vals );    // This line causes problems.   Array a2( a1 );    return 0;}
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 allocate memory for it. Thus, on deletion of a1, the destructor frees vals. All subsequent vals containing instances when trying to be deleted by the destructor causes the program to crash, as vals do 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.
References:
https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)
http://www.drdobbs.com/c-made-easier-the-rule-of-three/184401400
This article is contributed by Nihar Ranjan Sarkar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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. 
stevewillson
cpp-constructor
cpp-destructor
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Unordered Sets in C++ Standard Template Library
List in C++ Standard Template Library (STL)
std::find in C++
Inline Functions in C++