Jump to content

Special member functions

From Wikipedia, the free encyclopedia

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]).
  • 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).[4]

In C++03, before the introduction of move semantics (in C++11) the special member functions consisted of:[5]

Example

[edit]

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

import std;

using std::string;

class Explicit {
private:
    string message;
public:
    Explicit() {
        std::println("Default constructor {}");
    }

    explicit Explicit(string message):
        message{std::move(message)} {
        std::println("Non-default constructor {}", message);
    }

    Explicit(const Explicit& other) {
        std::println("Copy constructor {}", message);
        *this = other; // invoke copy assignment operator
    }

    Explicit& operator=(const Explicit& other) {
        std::println("Copy assignment operator {}", message);
        if (this != &other) {
            message = other.message;
        }
        return *this;
    }

    Explicit(Explicit&& other) noexcept {
        std::println("Move constructor {}", message);
        *this = std::move(other);  // invoke move assignment operator
    }

    Explicit& operator=(Explicit&& other) noexcept {
        std::println("Move assignment operator {}", message);
        if (this != &other) {
            message = std::move(other.message);
        }
        return *this;
    }

    ~Explicit() {
        std::println("Destructor {}", message);
    }
};

class Implicit : public Explicit {
private:
    Explicit member;
public:
    void spew() {
        std::println("Implicit({}, {})", message, member.message);
    }
};

Signatures

[edit]

Here are the signatures of the special member functions:

Function syntax for class X
Default constructor X();
Copy constructor X(const X& other);
Move constructor X(X&& other);
Copy assignment operator X& operator=(const X& other);
Move assignment operator X& operator=(X&& other);
Destructor ~X();

References

[edit]
  1. ^ ISO/IEC (2011). ISO/IEC 14882:2011 (3 ed.). ISO/IEC. pp. §12.
  2. ^ "Enforcing the Rule of Zero".
  3. ^ Except for the destructor if a base class already has a virtual destructor.
  4. ^ Similarly, the move constructor/assignment operators will receive && parameters instead of the alternatives.
  5. ^ ISO/IEC (1998). International Standard ISO/IEC 14882: Programming languages—C++ = Languages de programmation—C++ (1 ed.). ISO/IEC. pp. §12. OCLC 71718919.