liveBook · Manning

Big five

As explained in 100 C++ Mistakes and How to Avoid Them

The rule of five is a C++ class-design pattern in which a class considers defining a destructor, copy constructor, copy assignment operator, move constructor, and move assignment operator together.

The big five are a C++ class-design pattern: define the destructor, copy constructor, copy assignment operator, move constructor, and move assignment operator together when managing resources. Omitting part of the set can cause leaks, double ownership, expensive transfers, or unclear object state. section 2.1 section 7.6

A destructive copy

A legacy Page copy constructor deep-copies body but then deletes the original object's body and nulls its pointers. Writing Page p2 = p1 therefore behaves like an ownership transfer disguised as copying. A move constructor makes that transfer explicit and inexpensive: it takes the pointers, then neutralizes the moved-from Page.

Coordinate the special members around the class's ownership policy:

  • Release existing resources before replacement and delete owned resources in the destructor. section 7.6
  • Use move construction and move assignment for exclusive resource transfer, leaving moved-from objects neutralized. section 2.1

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Defines all five members and demonstrates raw-pointer and smart-pointer implementations, stopping short of a complete ownership redesign.
Examples
Uses Page and TextSection examples showing destructive copying, deep copying, pointer moves, and unique_ptr-based defaults.
What it assumes
Assumes basic C++ classes, pointers, constructors, assignment, and dynamic allocation while explaining move semantics through concrete ownership examples.
What you can do after
Apply the pattern to resource-owning classes and replace unsafe raw-pointer transfers with explicit move operations or smart pointers.
How it connects
Connects the big five with move semantics, deep copying, resource leaks, smart pointers, and RAII.

Also explained in

Compare how 2 books explain big five →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.