Rule of Three, Five & Zero
The special member functions
| Function | Signature |
|---|---|
| Default constructor | T() |
| Destructor | ~T() |
| Copy constructor | T(const T&) |
| Copy assignment | T& operator=(const T&) |
| Move constructor | T(T&&) noexcept |
| Move assignment | T& operator=(T&&) noexcept |
Rule of Three (C++98)
If a class needs a custom destructor, copy constructor or copy assignment, it almost certainly needs all three — because it manages a resource manually.
Rule of Five (C++11)
Add the move constructor and move assignment. If you declare any of the five, declare all five (or = default / = delete them deliberately).
⚠️ Declaring a destructor or copy operation suppresses the implicit move operations — your class silently falls back to (slower) copying.
Rule of Zero ✅ (the modern goal)
Don't write any of them. Use members that already manage themselves:
| Instead of | Use |
|---|---|
char* | std::string |
T* + size | std::vector<T> |
owning T* | std::unique_ptr<T> / std::shared_ptr<T> |
FILE* | std::fstream |
| Then the compiler-generated special members do exactly the right thing. |
= default and = delete
class Widget {
public:
Widget() = default; // ask for the generated version
Widget(const Widget&) = delete; // forbid copying
Widget(Widget&&) noexcept = default; // allow moving
};
Polymorphic base classes
Need virtual ~Base() = default; — which suppresses implicit moves. Common pattern: also default the other four (or delete copies to prevent slicing).
Which rule applies?
- Normal classes → Rule of Zero
- A class that wraps one raw resource (file handle, socket, C library object) → Rule of Five, and keep that class tiny
Example
#include <iostream>
#include <memory>
#include <string>
#include <type_traits>
#include <vector>
// Rule of Zero: every member manages itself, so no special members are written
class Customer {
public:
Customer(std::string name, std::vector<double> purchases)
: name_(std::move(name)), purchases_(std::move(purchases)) {}
const std::string& name() const { return name_; }
std::size_t count() const { return purchases_.size(); }
void add(double x) { purchases_.push_back(x); }
private:
std::string name_;
std::vector<double> purchases_;
};
// Rule of Zero with unique ownership: movable but not copyable, automatically
class Report {
public:
explicit Report(std::string title) : data_(std::make_unique<std::string>(std::move(title))) {}
const std::string& title() const { return *data_; }
private:
std::unique_ptr<std::string> data_;
};
// Declaring only a destructor silently removes the implicit move operations
class Legacy {
public:
~Legacy() {}
std::vector<int> data = std::vector<int>(1000);
};
// Polymorphic base: virtual destructor + explicitly defaulted rest
class Shape {
public:
virtual ~Shape() = default;
Shape() = default;
Shape(const Shape&) = delete; // prevent slicing copies
Shape& operator=(const Shape&) = delete;
virtual double area() const = 0;
};
int main() {
Customer a("Asha", { 500, 1200 });
Customer b = a; // generated copy: deep and correct
b.add(300);
std::cout << a.name() << " has " << a.count() << " purchases, copy has " << b.count() << '\n';
Report r1("November report");
Report r2 = std::move(r1); // generated move works
std::cout << "moved report: " << r2.title() << '\n';
std::cout << std::boolalpha;
std::cout << "Customer copyable? " << std::is_copy_constructible_v<Customer>
<< ", nothrow movable? " << std::is_nothrow_move_constructible_v<Customer> << '\n';
std::cout << "Report copyable? " << std::is_copy_constructible_v<Report>
<< ", movable? " << std::is_move_constructible_v<Report> << '\n';
std::cout << "Legacy nothrow movable? "
<< std::is_nothrow_move_constructible_v<Legacy> << " (falls back to copying)\n";
std::cout << "Shape copyable? " << std::is_copy_constructible_v<Shape> << '\n';
return 0;
}
/* Output:
Asha has 2 purchases, copy has 3
moved report: November report
Customer copyable? true, nothrow movable? true
Report copyable? false, movable? true
Legacy nothrow movable? false (falls back to copying)
Shape copyable? false
*/