What is a Copy Constructor in C++?
Learn what a C++ copy constructor is, when it runs, shallow vs deep copy, and the Rule of Three, with a clear deep-copy code example.
Expected Interview Answer
A copy constructor is a special constructor that creates a new object as a copy of an existing object of the same class, with the signature ClassName(const ClassName& other).
It is invoked when an object is initialized from another object, passed to a function by value, or returned by value. If you do not declare one, the compiler generates a default copy constructor that performs a member-wise (shallow) copy. For classes that own raw resources like heap memory or file handles, the shallow copy leaves two objects pointing at the same resource, causing double-frees; such classes need a user-defined copy constructor that performs a deep copy, following the Rule of Three (copy constructor, copy assignment operator, destructor).
- Enables safe duplication of objects
- Deep copy prevents shared-resource double-free bugs
- Compiler provides a default when a shallow copy is sufficient
- Central to correct pass-by-value and return-by-value semantics
- Anchors the Rule of Three for resource-owning classes
AI Mentor Explanation
A copy constructor is like making a brand-new, independent replica of a player's complete kit bag from an existing one — a fresh pair of pads, gloves, and bat, not just a label pointing at the original bag. A shallow copy would hand two players the same physical bag, so when one packs up and leaves with it, the other is suddenly left with nothing. A deep copy gives each player their own gear.
Step-by-Step Explanation
Step 1
Recognize the signature
A copy constructor takes a const reference to the same class: ClassName(const ClassName& other).
Step 2
Know when it fires
It runs on copy-initialization, passing an object by value, and returning an object by value.
Step 3
Understand the default
If undeclared, the compiler synthesizes one that copies each member (a shallow, member-wise copy).
Step 4
Spot the shallow-copy danger
For objects owning raw pointers, two copies share one resource, risking double-free on destruction.
Step 5
Write a deep copy
Allocate new storage and copy the pointed-to data so each object owns its own resource; honor the Rule of Three.
What Interviewer Expects
- Correct signature with const reference
- When the copy constructor is invoked
- Difference between shallow and deep copy
- The Rule of Three (and awareness of Rule of Five)
- Why const reference avoids infinite recursion and slicing
Common Mistakes
- Passing the argument by value instead of const reference
- Assuming the default copy constructor deep-copies pointers
- Forgetting the destructor and assignment operator (breaking Rule of Three)
- Confusing the copy constructor with the assignment operator
- Not handling self-safe copying of owned resources
Best Answer (HR Friendly)
“A copy constructor makes a new object that is a duplicate of an existing one. If the object holds its own memory, you usually write your own copy constructor so the copy gets its own separate memory instead of sharing it, which prevents crashes.”
Code Example
#include <iostream>
#include <cstring>
class Buffer {
char* data;
size_t size;
public:
Buffer(const char* text) {
size = std::strlen(text) + 1;
data = new char[size];
std::strcpy(data, text);
}
// Copy constructor: deep copy
Buffer(const Buffer& other) : size(other.size) {
data = new char[size]; // own memory
std::strcpy(data, other.data); // copy contents
}
~Buffer() { delete[] data; } // Rule of Three
const char* c_str() const { return data; }
};
int main() {
Buffer a("hello");
Buffer b = a; // copy constructor runs; b owns its own buffer
std::cout << b.c_str() << "\n";
return 0;
}Follow-up Questions
- What is the difference between a copy constructor and copy assignment operator?
- Explain the Rule of Three and the Rule of Five.
- Why must the parameter be a const reference and not by value?
- What is the difference between a shallow copy and a deep copy?
- How does a move constructor differ from a copy constructor?
MCQ Practice
1. What is the correct signature of a copy constructor for class T?
A copy constructor takes a const reference to the same type; taking it by value would cause infinite recursion.
2. What does the compiler-generated default copy constructor do?
The implicit copy constructor copies each member directly, which is a shallow copy for raw pointers.
3. The Rule of Three says that if you define one of these, you likely need all three. Which set is it?
The Rule of Three covers the copy constructor, copy assignment operator, and destructor for resource-owning classes.
Flash Cards
What is a copy constructor's signature? — ClassName(const ClassName& other) — it takes a const reference to another object of the same class.
When is a copy constructor invoked? — On copy-initialization (T b = a), passing an object by value, and returning an object by value.
Shallow vs deep copy? — Shallow copies pointer values so both objects share one resource; deep copy allocates new storage so each object owns its own.
What is the Rule of Three? — If a class needs a custom copy constructor, copy assignment operator, or destructor, it usually needs all three.