Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

The rule of three, five and zero

Reference for the C++ rule of three, rule of five and rule of zero: destructor, copy and move special members, when they are generated, =default and =delete.

A class has up to six special member functions that the compiler may write for you: the default constructor, destructor, copy constructor, copy assignment, move constructor and move assignment. The "rules" tell you when you must write them yourself.

Rule of three: if a class needs a custom destructor, copy constructor or copy assignment operator, it almost certainly needs all three, because the need comes from owning a resource. Rule of five adds the move constructor and move assignment (C++11). Rule of zero says the best class declares none of them: it holds members that manage themselves (std::string, std::vector, smart pointers), so the generated functions are correct.

C++
class Buffer {
public:
    ~Buffer();                          // destructor
    Buffer(const Buffer&);              // copy ctor
    Buffer& operator=(const Buffer&);   // copy assignment
    Buffer(Buffer&&) noexcept;          // move ctor
    Buffer& operator=(Buffer&&) noexcept; // move assignment
};

Try it

C++ Rule of five for an owning class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Rule of zero and a non-copyable class

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
~T();                          // destructor
T(const T&);                   // copy constructor
T& operator=(const T&);        // copy assignment
T(T&&) noexcept;               // move constructor
T& operator=(T&&) noexcept;    // move assignment

T(const T&) = default;         // ask for the generated version
T(const T&) = delete;          // forbid
Declared by you What the compiler still generates
Nothing All six (when members allow).
Any constructor Everything except the default constructor.
Destructor Copy operations (deprecated behavior); move operations are not generated.
Copy ctor or copy assignment The other copy operation (deprecated); moves are not generated, so moves fall back to copies.
Move ctor or move assignment Copies are defined as deleted; you must provide them if you need them.

Errors / undefined behavior: a class that owns a raw pointer but relies on the generated copy performs a shallow copy; both objects then delete the same memory (double free, undefined behavior). A self-assignment bug in a hand-written operator= can free the data it is about to copy.

Usage notes

  • Prefer the rule of zero. Hold resources in standard types or smart pointers. Then you write no special members, and the class is automatically correct, movable and exception-safe.
  • Rule of five means declare all or none. Declaring only a destructor suppresses the implicit moves, so "moving" quietly becomes an expensive copy. Spell out the others with = default if you want the generated behavior.
  • Mark moves noexcept. std::vector only uses the move constructor during growth if it cannot throw; otherwise it copies to preserve the strong guarantee.
  • Moved-from objects must stay valid. They may be destroyed or assigned to. Leave pointers null and sizes zero, as std::exchange does above.
  • Copy-and-swap gives the strong guarantee and handles self-assignment, at the cost of a temporary.
  • Polymorphic base classes need a virtual destructor; declare it = default and also default the other special members if you still want copy/move (or delete copying to avoid slicing).
  • = delete documents intent and gives clear compiler errors: use it for non-copyable resource handles.
  • Standard types: std::unique_ptr is move-only; std::shared_ptr is copyable and movable; std::mutex is neither.