Special member functions are six compiler-recognized operations governing default construction, copying, moving, assignment, and destruction: the default constructor, copy constructor, copy assignment, move constructor, move assignment, and destructor. Leaving them undeclared invokes the rule of zero, which often avoids unnecessary code and bugs. section 6.1.1 section 6.1.4
The six operations are:
- X() — default construction. section 6.1.1
- X(const X&) and operator=(const X&) — copying and copy assignment. section 6.1.1
- X(X&&) and operator=(X&&) — moving and move assignment. section 6.1.1
- ~X() — destruction. section 6.1.1
Inheritance changes the practical rules. A polymorphic base class should declare a virtual destructor so deleting a derived object through a base pointer destroys it correctly. A user-declared destructor can prevent implicit move operations; classes needing move semantics may therefore follow the rule of five and declare all five relevant operations. section 6.1.1 section 6.3
A base class with deliberate special members
A race base class can default its constructor and virtual destructor while deleting copying. Its pure virtual methods make it abstract, and derived blobs supply the behavior. Type traits can then verify that the class is destructible but not default-constructible, copyable, or movable. 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 base-class declaration showing a defaulted virtual destructor, default constructor, and deleted copy operations. section 6.1.1
