The rule of three is a rule of thumb in C++ that claims that if a classdefines any of the following then it should probably explicitly define all three:
These three functions are special member functions. If one of these functions is used without first being declared by the programmer it will be implicitly implemented by the compiler with the following default semantics:
Destructor – Call the destructors of all the object's class-type members
Copy constructor – Construct all the object's members from the corresponding members of the copy constructor's argument, calling the copy constructors of the object's class-type members, and doing a plain assignment of all non-class type data members
Copy assignment operator – Assign all the object's members from the corresponding members of the assignment operator's argument, calling the copy assignment operators of the object's class-type members, and doing a plain assignment of all non-class type data members.
The Rule of Three claims that if one of these had to be defined by the programmer, it means that the compiler-generated version does not fit the needs of the class in one case and it will probably not fit in the other cases either. The term "Rule of three" was coined by Marshall Cline in 1991. An amendment to this rule is that if the class is designed in such a way that Resource Acquisition Is Initialization is used for all its members, the destructor may be left undefined. A ready-to-go example of this approach is the use of smart pointers instead of plain ones. Because implicitly-generated constructors and assignment operators simply copy all class data members, one should define explicit copy constructors and copy assignment operators for classes that encapsulate complex data structures or have external references such as pointers, if you need to copy the objects pointed to by the class members. If the default behavior is actually the intended one, then an explicit definition, although redundant, will be "self-documenting code" indicating that it was an intention rather than an oversight. Modern C++ includes a syntax for expressly specifying that a default function is desired without having to type out the function body.
Rule of Five
With the advent of C++11the rule of three can be broadened to the rule of five as C++11 implements move semantics, allowing destination objects to grab data from temporary objects. The following example also shows the new moving members: move constructor and move assignment operator. Consequently, for the rule of five we have the following special members:
destructor
copy constructor
copy assignment operator
move constructor
move assignment operator
Situations exist where classes may need destructors, but cannot sensibly implement copy and move constructors and copy and move assignment operators. This happens, for example, when the base class does not support these latter Big Four members, but the derived class's constructor allocates memory for its own use. In C++11, this can be simplified by explicitly specifying the five members as default.