The Rule of
Three / Five
Master the C++ guidelines for resource management: when and why you must define copy constructors, assignment operators, and move semantics.
What is Resource Management?
In C++, objects can own resources: heap memory, file handles, network sockets, database connections, or GPU buffers. When an object owns a resource, it is responsible for acquiring and releasing that resource.
The Problem: Shallow Copies
By default, C++ copies objects member-by-member. If a member is a pointer, only the pointer value is copied, not the data it points to. Two objects now share the same resource.
Why Compiler Defaults Fall Short
The compiler auto-generates special member functions for every class. But when your class manages a raw resource, these defaults do the wrong thing.
delete, so memory leaks.The Rule of Three
If a class requires a user-defined implementation of any one of the following three, it almost certainly requires all three:
delete[] data).Extending to the Rule of Five
C++11 introduced move semantics: a way to transfer ownership of a resource instead of copying it. This is far more efficient for temporary objects.
Copy vs Move
Three vs Five vs Zero
| Rule of Three | Rule of Five | Rule of Zero | |
|---|---|---|---|
| C++ Standard | C++98 | C++11+ | C++11+ |
| Functions Defined | 3 | 5 | 0 |
| Owns Raw Resources? | Yes | Yes | No |
| Move Support? | No | Yes | Auto |
| Best Practice? | Legacy | Resource owners | Preferred |
The Five Special Functions
How to Apply the Rule
new, fopen, or acquire any external handle?delete[], fclose, etc.).Example: IntArray Class
A class that owns a dynamically allocated array of integers. It must follow the Rule of Five.
Copy Constructor & Assignment
Move Constructor & Assignment
When is Move Triggered?
Common Mistakes
operator= does not check for self-assignment (this != &other), it may delete its own data before copying.std::vector) will fall back to copying instead of moving if the move constructor is not noexcept.nullptr. Otherwise the destructor will double-free the resource.Summary: Decision Flowchart
Question 1 of 5
A class has a custom destructor that calls delete[] buffer. According to the Rule of Three, which other functions must also be user-defined?
Question 2 of 5
What does a move constructor typically do with the source object's resource pointer?
nullptr. This prevents double deletion when the source is later destroyed.Question 3 of 5
A class uses only std::vector and std::string members (no raw pointers). Which rule should it follow?
std::vector and std::string), the compiler-generated copy/move/destructor do the right thing automatically. This is the Rule of Zero.Question 4 of 5
Why should move constructors and move assignment operators be marked noexcept?
std::vector check whether the move constructor is noexcept. If it is not, they fall back to copying for exception safety during reallocation, losing the performance benefit of move semantics.Question 5 of 5
What happens if a class defines a custom destructor but uses the compiler-generated default copy constructor on an object with a raw pointer member?