TRT
Sign in

Core · Module 5 · Lesson 3 of 6

Copy Semantics: Deep Copies and the Rule of Three

Fix a leaking buffer class with a deep copy constructor, copy assignment, and destructor, and learn when the Rule of Three demands all three.

Pro — members onlyAbout 11 minutes

By the end you can

  • Write a deep copy constructor that duplicates a heap buffer element by element
  • Implement copy assignment with self-assignment and exception safety
  • Explain when the Rule of Three requires custom copy and destruction

Builds on Constructors, Destructors and the Rule of Zero.

01 · A question first

You copy a contact list to sort it, and the original scrambles too. Both objects point at one buffer, and the first destructor frees memory the second still uses. What must a copy do so the two lists evolve independently and free memory exactly once?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Why copying the pointer copies nothing
  • How a deep copy duplicates each element
  • Why the Rule of Three groups these operations
  • What it costs and where it still breaks
  • Where it is used

03 · Practice

Something wrong with this lesson? Report a problem