TRT
Sign in

Advanced · Module 10 · Lesson 2 of 6

The Rule of Five and When to Default It

Copy buffers deeply when duplication is wanted, move them cheaply on transfer, and express the choice with default and delete on MyVector and MyString.

Pro — members onlyAbout 10 minutes

By the end you can

  • List the five special operations and state when each one runs
  • Apply =default and =delete to express copy and move intent
  • Explain why a destructor forces review of the other four operations

Builds on Lvalues, Rvalues and Stealing Resources With Move.

01 · A question first

A vector of strings grows, reallocates, and keeps running with no leak and no crash. Another version double frees on the second push. Both define a destructor. What four extra decisions separate the version that survives from the version that corrupts the heap?

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

  • Start with the problem
  • How the idea works
  • When default and delete express intent
  • Why it is correct
  • The copy-and-swap idiom and exception safety
  • Rule of zero versus rule of five in practice
  • What it costs
  • Pitfalls and variations

03 · Practice

Something wrong with this lesson? Report a problem