01 / 21
3 / 5

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.

C++

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.

MyObject ptr, size owns Heap Memory new int[100] must be delete[]'d Stack / Automatic Heap / Dynamic

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.

obj_a ptr = 0xAB obj_b ptr = 0xAB Heap Data @ 0xAB [10, 20, 30, ...] Both point to same memory! delete[] called twice = Double Free / UB shallow copy

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.

Destructor
Default does nothing for pointers: no delete, so memory leaks.
Copy Constructor
Default copies pointer value (shallow): two owners, one resource.
Copy Assignment
Default overwrites pointer: leaks old resource, shares new one.

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:

1
Destructor: releases the owned resource (e.g., delete[] data).
2
Copy Constructor: creates a new object as a deep copy of another.
3
Copy Assignment Operator: replaces one object's resource with a deep copy of another's.
Destructor
\(\sim\texttt{Class()}\)
Copy Constructor
\(\texttt{Class(const Class\&)}\)
Copy Assignment
\(\texttt{operator=(const Class\&)}\)
Define one? Define all three.

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.

1
Destructor: same as before.
2
Copy Constructor: deep copy.
3
Copy Assignment Operator: deep copy + release old.
4
Move Constructor: steals resource from a temporary, leaving it in a valid empty state.
5
Move Assignment Operator: releases old resource, steals from a temporary.

Copy vs Move

Copy (Deep) src ptr = 0xAB dest ptr = 0xCD copy [10,20,30] [10,20,30] + Move (Transfer) src ptr = nullptr dest ptr = 0xAB move [10,20,30] emptied Two separate allocations Slower, but both valid Zero allocations Fast: pointer swap only

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

\(\sim\texttt{Class()}\)
Destructor: called when an object goes out of scope or is deleted.
\(\texttt{Class(const Class\&)}\)
Copy Constructor: builds a new object from an existing one.
\(\texttt{operator=(const Class\&)}\)
Copy Assignment: replaces contents with a copy of another object.
\(\texttt{Class(Class\&\&)}\)
Move Constructor: builds a new object by stealing from a temporary.
\(\texttt{operator=(Class\&\&)}\)
Move Assignment: replaces contents by stealing from a temporary.

How to Apply the Rule

1
Identify the resource: does your class call new, fopen, or acquire any external handle?
2
Write the destructor: release the resource (delete[], fclose, etc.).
3
Write the copy constructor: allocate a new resource and deep-copy contents from the source.
4
Write the copy assignment: handle self-assignment, release old resource, deep-copy the new one.
5
Write move constructor and move assignment: transfer the resource pointer, null out the source.

Example: IntArray Class

A class that owns a dynamically allocated array of integers. It must follow the Rule of Five.

class IntArray { private: int* data; size_t sz; public: // Constructor IntArray(size_t n) : data(new int[n]()), sz(n) {} // 1. Destructor ~IntArray() { delete[] data; } // 2-5: see next slides... };

Copy Constructor & Assignment

// 2. Copy Constructor IntArray(const IntArray& other) : data(new int[other.sz]), sz(other.sz) { std::copy(other.data, other.data + sz, data); } // 3. Copy Assignment (copy-and-swap) IntArray& operator=(IntArray other) { std::swap(data, other.data); std::swap(sz, other.sz); return *this; }

Move Constructor & Assignment

// 4. Move Constructor IntArray(IntArray&& other) noexcept : data(other.data), sz(other.sz) { other.data = nullptr; other.sz = 0; } // 5. Move Assignment IntArray& operator=(IntArray&& other) noexcept { if (this != &other) { delete[] data; data = other.data; sz = other.sz; other.data = nullptr; other.sz = 0; } return *this; }

When is Move Triggered?

Return Values
Returning a local object by value from a function: the compiler moves (or elides the copy entirely via NRVO).
std::move()
Explicitly casting an lvalue to an rvalue reference, signaling: "I no longer need this object."
Temporaries
Passing a temporary (e.g., IntArray(100)) to a function or constructor: it binds to the && overload.

Common Mistakes

Missing Self-Assignment Check
If operator= does not check for self-assignment (this != &other), it may delete its own data before copying.
Forgetting noexcept
STL containers (e.g., std::vector) will fall back to copying instead of moving if the move constructor is not noexcept.
Not Nulling the Source
After a move, the source's pointer must be set to nullptr. Otherwise the destructor will double-free the resource.

Summary: Decision Flowchart

Does your class own a resource? No Rule of Zero Use smart pointers Yes Using C++11 or later? No Rule of Three
\(\text{Dtor, Copy Ctor, Copy Assign}\)
Yes Rule of Five
\(+ \text{Move Ctor, Move Assign}\)

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?

Question 3 of 5

A class uses only std::vector and std::string members (no raw pointers). Which rule should it follow?

Question 4 of 5

Why should move constructors and move assignment operators be marked noexcept?

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?