Copy Constructor &
Assignment Operator
Learn how C++ copies objects behind the scenes: when constructors fire, when assignment kicks in, and how to write both correctly.
What Is an Object Copy?
In C++, every class instance lives in memory. When you need a second object with the same data, the compiler performs a copy. But how that copy happens depends on the context: are you creating a brand-new object, or overwriting an existing one?
When Does Each One Fire?
Why Should You Care?
C++ gives you low-level control over memory. If your class manages a raw pointer, the compiler's default copy just duplicates the pointer address: both objects point to the same memory. This is a shallow copy and it leads to crashes.
Shallow vs Deep Copy
The Copy Constructor Signature
void).The Assignment Operator Signature
*this: enables chaining like a = b = c.operator= is the function name: C++ lets you overload the = symbol.Initialization vs Assignment
Other Trigger Scenarios
The copy constructor also fires in these common situations:
std::vector or other container copies it.How to Write Both Correctly
other's resource into your new memory.self-assignment (this != &other). Then free your old memory, allocate new memory, and copy.*this from operator= so that chained assignment (a = b = c) works correctly.Rule of Three says you need all three: destructor, copy constructor, and operator=.Example: IntBox Class
Tracing Which Gets Called
The Self-Assignment Guard
What happens if someone writes x = x? Without the self-assignment check, operator= would delete x's own data, then try to copy from it: reading freed memory.
The Rule of Three
If your class needs a custom version of any one of these three, it almost certainly needs all three:
~MyClass()MyClass(const MyClass&)operator=(const MyClass&)Common Mistakes
operator= without if (this != &other) causes undefined behavior on x = x.MyClass(MyClass other) instead of a reference triggers infinite recursion: the parameter copy calls the copy constructor endlessly.operator= returns void, chained assignment like a = b = c will fail to compile or behave incorrectly.Copy Constructor vs Assignment
| Copy Constructor | Assignment Operator | |
|---|---|---|
| When it fires | Object is being created | Object already exists |
| Syntax trigger | MyClass b = a; | b = a; |
| Return type | None (constructor) | MyClass& |
| Self-assignment check | Not needed | Required |
| Free old memory? | No (nothing to free) | Yes |
| Part of Rule of Three? | Yes | Yes |
Question 1 of 5
Given: MyClass a; MyClass b = a;
Which special member function is called to create b?
Question 2 of 5
Why must the copy constructor's parameter be a const reference rather than passed by value?
Question 3 of 5
What does the self-assignment check in operator= protect against?
Question 4 of 5
The Rule of Three states that if you define any one of three special functions, you should define all three. Which set is correct?
Question 5 of 5
Consider: MyClass a(10); MyClass b(20); b = a;
What must operator= do with b's old data before copying a's data?