liveBook · Manning

Big three

As explained in Learn C++ by Example

The rule of three is a C++ resource-management guideline stating that a class defining a destructor, copy constructor, or copy assignment operator will generally need to define all three.

The big three is a C++ resource-management guideline: when a class defines a destructor, copy constructor, or copy assignment operator, it will generally need to define all three. Special handling for one operation usually means the other two need corresponding resource behavior. section 6.1.4

The guideline concerns these special member functions:

  • Destructor: releases or otherwise manages the owned resource. section 6.1.4
  • Copy constructor: defines how a copied object obtains an appropriate resource. section 6.1.4
  • Copy assignment operator: defines how assignment replaces or shares the resource safely. section 6.1.4
  • Since C++11, also consider move construction and move assignment; otherwise containers may lose move-based optimization opportunities. section 6.1.4
Listing 6.10 Type traits to check for special member functions
#include <type_traits>
void check_properties()
{
    ...                                               #1
    static_assert(
        !std::is_default_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
         std::is_destructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_copy_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_copy_assignable_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_move_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_move_assignable_v<Race::Blob>
    );                                                #2
    static_assert(
         std::has_virtual_destructor_v<Race::Blob>
    );                                                #3
}

Inspect compile-time checks for whether a type is destructible, copyable, movable, or constructible. section 6.1.4

How Learn C++ by Example covers it

How deep it goes
Defines the guideline and links it to move operations, but stops short of implementing ownership or copy behavior.
Examples
Uses an abstract Blob type, static type-trait assertions, and vector push_back overloads to expose copying and moving behavior.
What you can do after
Evaluate whether a class supports destruction, copying, moving, and construction through C++ type traits.
How it connects
Connects destructors, copy construction, copy assignment, move construction, move assignment, type traits, and polymorphism.

Also explained in

Compare how 3 books explain big three →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.