Loading AI tools
In C++, functions which the compiler will generate automatically if not declared 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:
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]
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_;
};
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(); |
In C++03 before the introduction of move semantics (in C++11) the special member functions[5] were:
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.