Rule Of Three (C Programming)
   HOME

TheInfoList



OR:

The rule of three and rule of five are rules of thumb in C++ for the building of
exception-safe Exception safety is the state of code working correctly when exceptions are thrown. To aid in ensuring exception safety, C++ standard library developers have devised a set of ''exception safety levels'', contractual guarantees of the behavior of a ...
code and for formalizing rules on resource management. The rules prescribe how the default members of a class should be used to achieve these goals systematically.


Rule of three

The rule of three (also known as ''the law of the big three'' or ''the big three'') is a
rule of thumb In English, the phrase ''rule of thumb'' refers to an approximate method for doing something, based on practical experience rather than theory. This usage of the phrase can be traced back to the 17th century and has been associated with various t ...
in C++ (prior to C++11) that claims that if a class defines any of the following then it should probably explicitly define all three: * destructor *
copy constructor In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required ...
*
copy assignment operator Copy may refer to: *Copying or the product of copying (including the plural "copies"); the duplication of information or an artifact **Cut, copy and paste, a method of reproducing text or other data in computing **File copying **Photocopying, a pr ...
These three functions are
special member functions Special member functions in C++ are functions which the compiler will automatically generate if they are used, but not declared explicitly by the programmer. The automatically generated special member functions are: * Default constructor if no o ...
. 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 (e.g., ''int'' or pointer) 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 (e.g. ''int'' or pointer) 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 (RAII) is used for all its (nontrivial) members, the destructor may be left undefined (also known as The Law of The Big Two). A ready-to-go example of this approach is the use of
smart pointer In computer science, a smart pointer is an abstract data type that simulates a pointer while providing added features, such as automatic memory management or bounds checking. Such features are intended to reduce bugs caused by the misuse of poin ...
s instead of plain ones. Because implicitly-generated constructors and assignment operators simply copy all class data members ("
shallow copy In object-oriented programming, object copying is creating a copy of an existing object, a unit of data in object-oriented programming. The resulting object is called an ''object copy'' or simply ''copy'' of the original object. Copying is basic bu ...
"), 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 ("shallow copy") 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 In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure ( constituency) ...
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++11 the rule of three can be broadened to ''the rule of five'' (also known as "the rule of the big five") as C++11 implements ''
move semantics C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
'', allowing destination objects to ''grab'' (or ''steal'') 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 In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required ...
*
copy assignment operator Copy may refer to: *Copying or the product of copying (including the plural "copies"); the duplication of information or an artifact **Cut, copy and paste, a method of reproducing text or other data in computing **File copying **Photocopying, a pr ...
*
move constructor In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required ...
*
move assignment operator In the C++ programming language, the move assignment operator = is used for transferring a temporary object to an existing object. The move assignment operator, like most C++ operators, can be overloaded. Like the copy assignment operator it is a ...
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.


See also

* C++ classes * Class (computer programming)


References

{{C++ programming language C++ Computer programming folklore Software engineering folklore Articles with example C++ code