IT lexicon › Programming › Rule of five

Rule of five

Programming På svenska → Updated: 2026-07-29

If a class needs its own destructor it almost certainly also needs its own copy constructor, copy assignment and — since C++11 — move constructor and move assignment.

The reasoning is simple: needing any one of them means the class manages a resource, and the compiler's automatically generated versions of the others will then do the wrong thing — shallow-copying a pointer so two objects believe they own the same memory, and freeing it twice. Originally the rule was the rule of three and covered destructor, copy constructor and copy assignment. C++11 added the two move operations, and with them an unpleasant trap: merely declaring a destructor suppresses the implicitly generated move operations. A class following the old rule of three therefore silently gets copies where it should get moves — still correct, but measurably slower and easy to miss.

← Back to the lexicon