Classes and objects · core
Copy constructor, deep copy and the rule of three
The compiler's copy copies every member. For a raw pointer that is a shallow copy: two objects share one block and both delete it. A class that owns a resource needs its own copy constructor, copy assignment and destructor, or better, none of them.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Copy constructor:
Student(const Student& other). It runs when a new object is made from an existing one:Student b = a;,Student b(a);, and when an object is passed by value. Student b = a;calls the copy constructor, because b is new.b = a;on an existing b calls the copy assignment operator, operator=.- The parameter must be a reference. Taken by value, the call would need a copy, which would call the copy constructor again, forever; the compiler rejects it.
- The default copy copies each member. With a raw pointer member that is a shallow copy: both objects point to one block, and both destructors delete it (a double delete, which is undefined).
- Deep copy: the copy constructor allocates its own block and copies the data into it. The copy assignment must also free the old block and handle
a = a;safely. - Rule of three: if a class needs its own destructor, copy constructor or copy assignment, it almost certainly needs all three. Rule of five (C++11) adds the move constructor and the move assignment.
- Rule of zero: hold members like std::string, std::vector or std::unique_ptr, which manage themselves, and write none of the five.
- Returning an object by value often makes no copy. Since C++17,
return Student(...);builds the object directly in the caller (guaranteed copy elision); returning a named local may also be elided, or moved.
The red pen
Common traps: mistake → fix
Mistake:
Student b = a;calls operator=Fix: it calls the copy constructor, because b is a new object.
Mistake: The default copy constructor makes a deep copy
Fix: it copies the pointer, not what it points to.
Mistake: The copy constructor can take its parameter by value
Fix: it would call itself forever; it must be a reference.
Mistake: The rule of three means writing three constructors
Fix: it means the destructor, the copy constructor and the copy assignment.
Easy to hard
Questions interviewers ask, easy to hard
- What is a copy constructor, and when is it called?
- What is the difference between a shallow copy and a deep copy?
- Why must the copy constructor take its parameter by reference?
- What is the rule of three? What do the rule of five and the rule of zero add?
- A class holds
char* nameand the program crashes with "double free" at the end of main. Why?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.