Special member functions

Short description: In C++, functions which the compiler will generate automatically if not declared

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:

* Default constructor if no other constructor is explicitly declared.
* Copy constructor if no move constructor and move assignment operator are explicitly declared.

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.

* Move assignment operator if no copy constructor, copy assignment operator, move constructor and destructor are explicitly declared.
* Destructor

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]

## Contents

* 1 Example
* 2 Signatures
* 3 C++03
* 4 References

## Example

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

#include <iostream>
#include <string>
#include <utility>

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:

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();`

## C++03

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

* Default constructor (if no other constructor is explicitly declared)
* Copy constructor
* Copy assignment operator
* Destructor

## References

1. ↑ ISO/IEC (2011). ISO/IEC 14882:2011 (3 ed.). ISO/IEC. pp. §12.
2. ↑ "Enforcing the Rule of Zero". http://accu.org/index.php/journals/1896.
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.

* v
* t
* e

C++

* C++
* C++98
* C++03
* C++11
* C++14
* C++17
* C++20
* C++23

Features|

* Classes
* Exception handling
* Function overloading
* new and delete
* Operator overloading
* Operators
* Templates

Standard Library|

* I/O Streams
* Smart pointers
* STL
* Strings

Compilers|

* Comparison of C++ compilers
* Borland C++
* Borland Turbo C++
* C++Builder
* Clang
* GCC
* Intel C++ Compiler
* Oracle Solaris Studio
* Visual C++
* Watcom C/C++

IDEs|

* Comparison of C IDEs
* Anjuta
* CLion
* Code
* CodeLite
* Eclipse
* Geany
* Microsoft Visual Studio
* NetBeans
* KDevelop
* Qt Creator

Superset languages|

* Objective-C++
* C++/CLI
* ( C++/CX )
* Ch
* SYCL

Dialects|

* Embedded C++

Relative to
other languages|

* Compatibility of C and C++
* Comparison of Java and C++
* Comparison of ALGOL 68 and C++
* Comparison of programming languages

* Category

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Special member functions. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Special_member_functions&oldid=4156187"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template