Rule of Three/Five
Manage copy and move correctly.
Rule of Three/Five is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Rule of Three/Five” lesson free?
Yes — the full text of “Rule of Three/Five” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
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
- The RAII Principle
- Destructors and Cleanup
- Rule of Three/Five
- Scope Guards