1. Problem It Solves
Resources must be released on every path, including early returns and exceptions. RAII binds cleanup to object lifetime; the Rule of Five identifies special operations a manual resource owner may need, while the Rule of Zero delegates them to reliable member types.
Focus on the smallest useful form, its observable behavior, and its safety boundary.
2. Prerequisites
Days 3, 11, and 18: ownership, moves, destructors, scopes, and smart pointers.
3. Core Idea
An object's lifetime is a bracket: construction acquires a valid state and destruction closes it. Prefer members such as std::vector and std::unique_ptr so generated copy, move, and destruction behavior does the right work.
Identify the objects and types, today's operation, and the printed result. This connects syntax to behavior.
4. Minimal Syntax
struct Buffer {
std::vector<int> data; // Rule of Zero
};5. How It Works
A scope creates a value-owning buffer whose vector acquires storage during construction.
Copying duplicates vector elements and moving transfers vector state through already-correct library special members.
Leaving scope destroys every buffer and releases its storage automatically without manual
delete.
6. Common Mistakes
Manually defining one ownership-related special member while forgetting the others can cause leaks, double release, or accidentally disabled moves.
Do not copy the pattern without checking construction, destruction order, copy versus move semantics, self-assignment, and member ownership. A program may compile while still having the wrong lifetime, ownership, invalidation, ordering, or performance behavior.
7. When to Use It
Use it when an object should maintain a valid resource invariant for exactly its lifetime.
Avoid it when manual special-member code merely repeats behavior already provided by standard RAII members.
8. Simple Example
A Buffer contains only a vector, so it follows the Rule of Zero. The program copies one buffer, moves the copy, and lets scope exit perform all cleanup.
The .cpp file uses fixed data. Predict its output, compile it, then change one value and test the prediction.
Complete sample code
Source file
cpp14/21_object_lifetime_raii_rule_of_five_zero/main.cpp
#include <iostream>
#include <utility>
#include <vector>
struct Buffer {
std::vector<int> data; // vector provides Rule-of-Zero ownership
};
int main() {
Buffer original{{1, 2, 3}};
Buffer copy = original;
Buffer moved = std::move(copy);
std::cout << "original size: " << original.data.size() << "\n";
std::cout << "moved size: " << moved.data.size() << "\n";
} // every vector releases its own storage
9. Key Takeaways
Rule of Zero is the default; write the Rule of Five only when the class truly manages a raw resource boundary.
An object's lifetime is a bracket: construction acquires a valid state and destruction closes it. Prefer members such as
std::vectorandstd::unique_ptrso generated copy, move, and destruction behavior does the right work.The compiler or library follows a precise rule; verify construction, destruction order, copy versus move semantics, self-assignment, and member ownership.
Prefer the smallest form that communicates intent and measure costs when performance matters.
10. Self-Check Questions
Easy — What is the main purpose of Object Lifetime, RAII, Rule of Five, and Rule of Zero?
Medium — After moving the copied vector into
moved, which object owns the elements used for output?Hard — Why can adding only a user-declared destructor change implicit move generation even when the destructor body looks harmless?