1. Problem It Solves
A copy constructor creates a new object from an existing object and defines whether owned state is duplicated safely. It makes an important constraint visible instead of leaving readers to guess. This lesson keeps only the C++11 core that fits one focused day.
2. Prerequisites
The ideas from Day 13, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: A copy constructor creates a new object from an existing object and defines whether owned state is duplicated safely. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
Box(const Box& other) : value_(other.value_) {}5. How It Works
The example creates a tiny fixed state with no keyboard input.
C++11 or the standard-library contract applies today's rule.
The program prints the important result so it can be checked against the source.
6. Common Mistakes
A shallow copy of an owning raw pointer makes two objects delete the same allocation, causing undefined behavior.
7. When to Use It
Use it when a type owns state that needs explicit duplication semantics.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
Copying a small Box invokes its copy constructor, then changing the copy shows that the original remains independent. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/14_copy_constructor/main.cpp
#include <iostream>
class Box {
public:
explicit Box(int value) : value_(value) {}
Box(const Box& other) : value_(other.value_) {
std::cout << "copied\n";
}
void set(int value) {
value_ = value;
}
int get() const {
return value_;
}
private:
int value_;
};
int main() {
Box original(7);
Box copy(original);
copy.set(9);
std::cout << "original=" << original.get() << " copy=" << copy.get() << '\n';
}
9. Key Takeaways
The feature is part of the C++11 scope used in this course.
Understand its lifetime, ownership, type, and ordering consequences.
Compile with warnings and prefer the smallest form that makes the rule obvious.
10. Self-Check Questions
Easy — Does a copy constructor create a new object or modify an existing one? Explain
Box(const Box& other).Medium — Read the small example described above. What value or state should it print, and which rule produces that result?
Hard — Find and explain the subtle bug in this situation: A shallow copy of an owning raw pointer makes two objects delete the same allocation, causing undefined behavior. What is the smallest C++11-safe correction?