Special member functions

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

Special member functions[1] in C++ 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]).
  • Move constructor if no copy constructor, copy assignment operator, move assignment operator and destructor are explicitly declared.
  • Copy assignment operator if no move constructor and move assignment operator are explicitly declared.
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).

Example

The following example depicts two classes: Explicit for which all C++98 special member functions are explicitly declared and Implicit for which none are declared.

#include <iostream>

class Explicit {
    friend class Implicit;
    string msg;
public:
    Explicit() : msg("") 
    {
        std::cout << "Default constructor " << msg << '\n';
    }

    Explicit(const string& value) : msg(value) 
    {
        std::cout << "Non-default constructor " << msg << '\n';
    }

    Explicit(const Explicit& other) : msg(other.msg) 
    {
        std::cout << "Copy constructor " << msg << '\n';
    }

     Explicit& operator=(const Explicit& other) 
    {
        std::cout << "Copy assignment operator " << msg << '\n';
        if (this != &other) {
            msg = other.msg;
        }
        return *this;
    }
     Explicit(Explicit&& other) : msg(other.msg) 
    {
        std::cout << "Move constructor " << msg << '\n';
    }

     Explicit& operator=(Explicit&& other) 
    {
        std::cout << "Move assignment operator " << msg << '\n';
        if (this != &other) {
            msg = other.msg;
        }
        return *this;
    }

    
    ~Explicit() 
    {
        std::cout << "Destructor " << msg << '\n';
    }
};

class Implicit : public Explicit {
    int i;
    void* p;
    Explicit member;
public:
    void Spew() 
    { 
        std::cout << "Implicit(" << msg << ", " << member.msg << ")\n"; 
    }
};

Signatures

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 ~MyClass();

C++98

In C++98 before the introduction of move semantics the special member functions[4] were:

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. http://accu.org/index.php/journals/1896
  3. Except for the destructor if a base class already has a virtual destructor.
  4. Lua error in package.lua at line 80: module 'strict' not found.