In the C++ programming language, special member functions[1] are functions which the compiler will automatically generate if they are used, but not declared explicitly by the programmer.The automatically generated special member functions are:
If a destructor is declared generation of a copy constructor is deprecated (C++11, proposal N3242[2]).
If a destructor is declared, generation of a copy assignment operator is deprecated.
In these cases the compiler generated versions of these functions perform a memberwise operation. For example, the compiler generated destructor will destroy each sub-object (base class or member) of the object.
The compiler generated functions will be public
, non-virtual[3] and the copy constructor and assignment operators will receive const&
parameters (and not be of the alternative legal forms).[4]
The following example depicts two classes: for which all special member functions are explicitly declared and for which none are declared.
class Explicit ;
class Implicit : public Explicit ;
Here are the signatures of the special member functions:
Function | syntax for class MyClass | |
---|---|---|
Default constructor | MyClass; | |
Copy constructor | MyClass(const MyClass& other); | |
Move constructor | MyClass(MyClass&& other) noexcept; | |
Copy assignment operator | MyClass& operator=(const MyClass& other); | |
Move assignment operator | MyClass& operator=(MyClass&& other) noexcept; | |
Destructor | virtual ~MyClass; |
In C++03 before the introduction of move semantics (in C++11) the special member functions[5] were:
&&
parameters instead of the alternatives.