liveBook · Manning

Rule of zero

As explained in Learn C++ by Example

The rule of zero is a C++ design guideline in which a class declares none of the special member functions so the compiler can provide appropriate defaults.

The rule of zero is a C++ design guideline: declare none of a class’s six special member functions and let the compiler provide their defaults. The six are the default constructor, copy constructor, copy assignment, move constructor, move assignment, and destructor. Less handwritten code can mean fewer bugs, especially when the class does not manage memory or resource handles. section 6.1.1 section 6.3

Apply the guideline with these practical qualifications:

  • Keep the defaults when a class owns no resources requiring custom cleanup. section 6.1.1
  • Treat polymorphic base classes differently: a base used through pointers needs a virtual destructor, even though declaring it means the class is no longer a strict rule-of-zero example. section 6.1.1 section 6.3
  • Remember that declaring a destructor or another special member can block implicitly generated move operations; explicitly declare or delete the needed members when moving or copying matters. section 6.1.4
  • Use type traits such as std::is_move_constructible_v and std::is_copy_assignable_v to check the resulting type properties. section 6.1.4

A polymorphic Blob base class

A first Blob base class declares only pure virtual step and total_steps functions, so its six special members are defaulted. Because a derived Blob may be deleted through a base pointer, the refined version adds virtual ~Blob() = default and explicitly deletes copying. This illustrates the boundary between accepting defaults and making deliberate special-member choices. section 6.1.1 section 6.1.4

Listing 6.2 A better base class
#pragma once
 
namespace Race
{
    class Blob
    {
    public:
        virtual ~Blob() = default;                #1
        Blob() = default;                         #2
        Blob(Blob const&) = delete;               #3
        Blob& operator=(Blob const&) = delete;    #3
 
        virtual void step() = 0;
        virtual int total_steps() const = 0;
    };
}

Inspect the refined Blob declaration showing a defaulted virtual destructor and deleted copy operations. section 6.1.1

How Learn C++ by Example covers it

How deep it goes
Explains all six special members, virtual-destructor consequences, move suppression, rule of five, and compile-time trait checks.
Examples
Demonstrates the guideline with Blob subclasses, polymorphic deletion, deleted copy operations, and static type-trait assertions.
What you can do after
Evaluate which special members a class should expose and verify the result with standard type traits.
How it connects
Connects special member functions with polymorphism, virtual destructors, move semantics, rule of five, type traits, and smart pointers.

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.