From Surf Wiki (app.surf) — the open knowledge base
Rule of three (C++ programming)
Rules of thumb in C++
Rules of thumb in C++
The rule of three and rule of five are rules of thumb in C++ for the building of exception-safe 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 C++ (prior to C++11) that claims that if a class defines any of the following then it should probably explicitly define all three:{{cite book | url-access = limited
- destructor
- copy constructor
- copy assignment operator
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 (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 states 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{{cite web
Because implicitly-generated constructors and assignment operators simply copy all class data members ("shallow copy"),{{cite book
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,{{cite web
- 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.
References
References
- Koenig, Andrew. (2001-06-01). "C++ Made Easier: The Rule of Three".
- "C++11: The Rule of the Big Five".
- "The rule of three/five/zero". cppreference.com.
This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.
Ask Mako anything about Rule of three (C++ programming) — get instant answers, deeper analysis, and related topics.
Research with MakoFree with your Surf account
Create a free account to save articles, ask Mako questions, and organize your research.
Sign up freeThis content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.
Report