All Courses
OOP: RAII AND OWNERSHIP

Rule of Three and Five

Concept Definition

The Rule of Three says that if a class manually defines a destructor, copy constructor, or copy assignment operator, it probably needs all three. The Rule of Five adds move constructor and move assignment operator. These rules usually appear when a class directly owns a raw resource.

The analogy is making duplicate keys for a rented room. If copying one object creates another owner, you must decide who is allowed to return the key.

Why It Matters

Raw ownership creates dangerous default behavior. The compiler's default copy operation copies pointer values, not the heap object behind the pointer. Two objects may then believe they own the same memory, causing double deletion or corrupted state.

Modern C++ usually prefers Rule of Zero, but you still need to understand Rule of Three and Five to read older code and recognize ownership bugs.

Syntax Block

class Owner {
public:
    ~Owner();                          // destructor
    Owner(const Owner& other);         // copy constructor
    Owner& operator=(const Owner& other); // copy assignment
    Owner(Owner&& other) noexcept;     // move constructor
    Owner& operator=(Owner&& other) noexcept; // move assignment
};

Explained Code

Example: Prefer Moving Ownership With unique_ptr

#include <memory> // std::unique_ptr
#include <utility> // std::move

class Buffer {
private:
    std::unique_ptr<int[]> data;       // exclusive array ownership
    std::size_t size = 0;              // number of elements

public:
    explicit Buffer(std::size_t count)
        : data(std::make_unique<int[]>(count)), size(count) {} // allocate safely

    Buffer(const Buffer&) = delete;            // copying would need deep-copy policy
    Buffer& operator=(const Buffer&) = delete; // copying disabled clearly

    Buffer(Buffer&&) noexcept = default;       // moving transfers ownership
    Buffer& operator=(Buffer&&) noexcept = default; // move assignment is safe
};

This class avoids raw new[] and delete[]. It also makes the copy policy explicit instead of allowing accidental shallow copies.

Key Points / Rules

  • Rule of Three protects classes with manual raw ownership.
  • Rule of Five adds move operations for efficient ownership transfer.
  • Prefer Rule of Zero when standard library types can manage the resource.
  • Delete copy operations when copying ownership does not make sense.
  • Mark move operations noexcept when defaulted moves are safe.

Common Mistakes

  1. Writing a destructor but forgetting copy assignment. This often creates double-delete bugs.
  2. Allowing shallow pointer copies. Copying an address is not the same as copying the owned resource.
  3. Writing complex ownership code instead of using smart pointers. Standard types are safer.
  4. Forgetting self-assignment and exception safety. Manual assignment operators are easy to get wrong.

Quick Check

  1. Why does raw pointer ownership trigger Rule of Three concerns?
  2. Why does Buffer delete copy operations?

Viva Answer

Rule of Three and Rule of Five warn that manual resource ownership needs explicit copy, move, and destruction behavior. In modern C++, prefer Rule of Zero whenever possible.