Special member functions


In the C++ programming language, special member functions 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:
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 of the object.
The compiler generated functions will be public, non-virtual and the copy constructor and assignment operators will receive const& parameters.

Example

The following example depicts two classes: for which all special member functions are explicitly declared and for which none are declared.

  1. include
  2. include
  3. include
class Explicit ;
class Implicit : public Explicit ;

Signatures

Here are the signatures of the special member functions:
Functionsyntax for class MyClass
Default constructorMyClass;
Copy constructorMyClass;
Move constructorMyClass noexcept;
Copy assignment operatorMyClass& operator=;
Move assignment operatorMyClass& operator= noexcept;
Destructorvirtual ~MyClass;

C++03

In C++03 before the introduction of move semantics the special member functions were:
  • Default constructor
  • Copy constructor
  • Copy assignment operator
  • Destructor