special member functions

{{short description|In C++, functions which the compiler will generate automatically if not declared}}

{{Use dmy dates|date=December 2020}}

In the C++ programming language, special member functions{{cite book

| last = ISO/IEC

| author-link = International Organization for Standardization

| title = ISO/IEC 14882:2011

| edition = 3

| publisher = ISO/IEC

| year = 2011

| pages = §12 }} 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{{Cite web|url=http://accu.org/index.php/journals/1896|title=Enforcing the Rule of Zero}}).

  • 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-virtualExcept for the destructor if a base class already has a virtual destructor. and the copy constructor and assignment operators will receive const& parameters (and not be of the alternative legal forms).Similarly, the move constructor/assignment operators will receive && parameters instead of the alternatives.

Example

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

  1. include
  2. include
  3. include

class Explicit {

public:

Explicit() { std::cout << "Default constructor " << message_ << '\n'; }

explicit Explicit(std::string message) : message_(std::move(message)) {

std::cout << "Non-default constructor " << message_ << '\n';

}

Explicit(const Explicit& other) {

std::cout << "Copy constructor " << message_ << '\n';

*this = other; // invoke copy assignment operator

}

Explicit& operator=(const Explicit& other) {

std::cout << "Copy assignment operator " << message_ << '\n';

if (this != &other) {

message_ = other.message_;

}

return *this;

}

Explicit(Explicit&& other) noexcept {

std::cout << "Move constructor " << message_ << '\n';

*this = std::move(other); // invoke move assignment operator

}

Explicit& operator=(Explicit&& other) noexcept {

std::cout << "Move assignment operator " << message_ << '\n';

if (this != &other) {

message_ = std::move(other.message_);

}

return *this;

}

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

private:

friend class Implicit;

std::string message_;

};

class Implicit : public Explicit {

public:

void Spew() {

std::cout << "Implicit(" << message_ << ", " << member_.message_ << ")\n";

}

private:

Explicit member_;

};

Signatures

Here are the signatures of the special member functions:

class="wikitable"
Functionsyntax for class MyClass
Default constructorMyClass();
Copy constructorMyClass(const MyClass& other);
Move constructorMyClass(MyClass&& other) noexcept;
Copy assignment operatorMyClass& operator=(const MyClass& other);
Move assignment operatorMyClass& operator=(MyClass&& other) noexcept;
Destructorvirtual ~MyClass();

C++03

In C++03 before the introduction of move semantics (in C++11) the special member functions{{cite book

| last = ISO/IEC

| author-link = International Organization for Standardization

| title = International Standard ISO/IEC 14882: Programming languages—C++ = Languages de programmation—C++

| edition = 1

| publisher = ISO/IEC

| year = 1998

| oclc = 71718919

| pages = §12 }} were:

References

{{reflist}}

{{C++ programming language}}

Category:C++

Category:Articles with example C++ code