C++ Academy · Lesson

Rule of Three/Five

Manage copy and move correctly.

Lesson 3 of 413 steps

Rule of Three/Five is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

The Rule of Three

If a class needs a custom destructor, it almost always also needs a custom copy constructor and copy assignment. These three travel together.

Why Three?

A class managing a raw resource must define how it is destroyed, copied, and assigned — otherwise the compiler-generated shallow copies cause double frees.

#include <iostream>

class Bad {
    int* p;
public:
    Bad(int v) : p(new int(v)) {}
    ~Bad() { delete p; }
    // default copy => two objects delete the same p!
};

Custom Copy Constructor

A correct copy constructor performs a deep copy, allocating its own resource rather than sharing.

#include <iostream>

class Box {
    int* p;
public:
    Box(int v) : p(new int(v)) {}
    Box(const Box& o) : p(new int(*o.p)) {}   // deep copy
    ~Box() { delete p; }
    int get() const { return *p; }
};

int main() {
    Box a(5);
    Box b = a;   // independent copy
    std::cout << b.get() << "\n";
}

Custom Copy Assignment

Copy assignment must free the old resource, guard against self-assignment, and deep-copy the new value.

Box& operator=(const Box& o) {
    if (this != &o) {              // self-assignment guard
        delete p;
        p = new int(*o.p);
    }
    return *this;
}

The Rule of Five

C++11 adds move operations. If you define any of the five (destructor, copy ctor, copy assign, move ctor, move assign), consider defining all five.

Move Constructor

A move constructor steals the resource from a temporary and leaves the source empty — far cheaper than copying.

Box(Box&& o) noexcept : p(o.p) {
    o.p = nullptr;   // source left empty
}

Move Assignment

Move assignment releases the current resource then takes ownership of the source's.

Box& operator=(Box&& o) noexcept {
    if (this != &o) {
        delete p;
        p = o.p;
        o.p = nullptr;
    }
    return *this;
}

Mark Moves noexcept

Move operations should be noexcept so containers like std::vector can use them safely when reallocating.

The Rule of Zero

Best of all: own nothing raw. Use std::unique_ptr, std::vector, or std::string and you can define none of the five — the compiler defaults are correct.

#include <memory>

class Good {
    std::unique_ptr<int> p;   // owns the resource for you
public:
    Good(int v) : p(std::make_unique<int>(v)) {}
    // no destructor, no copy/move needed
};

Deleting Operations

If copying makes no sense, delete it: Box(const Box&) = delete;. This makes the type move-only or non-copyable on purpose.

Choosing the Right Rule

Aim for the Rule of Zero. Fall back to the Rule of Five only when you genuinely wrap a raw resource the standard library does not cover.

Quick Check

Test your understanding of the Rule of Three/Five.

Recap

You learned the Rule of Three/Five: managing a raw resource means defining destructor, copy, and move operations consistently. Mark moves noexcept. Best of all, follow the Rule of Zero by using standard RAII types and defining none.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Rule of Three/Five” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Rule of Three/Five”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Rule of Three/Five”?

Manage copy and move correctly. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Rule of Three/Five” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The RAII Principle
  2. Destructors and Cleanup
  3. Rule of Three/Five
  4. Scope Guards
← Back to C++ Academy