Special Member functions, are a type of function that the C++ Compiler automatically generates for a Class if no explicit declaration is given.
There are a total of 6 special Member Functions in Modern C++, listed below:
- Default Constructor
- Destructor
- Copy Constructor
- Move Constructor
- Copy Assignment Operator
- Move Assignment Operator
Before the introduction of Move Semantics in C++11, which introduced the Move Constructor and Move Assignment Operator, there are only 4 Special Member functions.
Special Member Functions have many rules, and the generation of certain special member functions may change based on certain factors. For example, if the Destructor is explicitly generated by the user, then the Move Constructor and Move Assignment Operator functions will not be auto-generated.
– | Default constructor | Copy constructor | Copy operator= | Move constructor | Move operator= | Destructor |
---|---|---|---|---|---|---|
Nothing | YES | YES | YES | YES | YES | YES |
Any constructor | NO | YES | YES | YES | YES | YES |
Default constructor | NO | YES | YES | YES | YES | YES |
Copy constructor | NO | NO | YES | NO | NO | YES |
Copy operator= | YES | YES | NO | NO | NO | YES |
Move constructor | NO | DELETED | DELETED | NO | NO | YES |
Move operator= | YES | DELETED | DELETED | NO | NO | YES |
Destructor | YES | YES | YES | NO | NO | NO |
Special Member Functions Rules in C++
A bunch of rules about the generation of special member functions. Basically just a written version of what is shown in the above table.
Default Constructor.
Upon the explicit creation of any Constructor, whether it be a parameterized or not, the default constructor will not be generated automatically.
Destructor
The Destructor is the only special member function that automatically generates in all situations, except when we define the Destructor itself. (It’s generation is not dependent on the other member functions)
Copy Constructor
If a destructor is explicitly declared, then the generation of the copy constructor is deprecated (C++11 onwards). It is still generated (as of C++20), but it may be removed entirely in a future release.
On the other hand, if either the move constructor or move assignment operator are explicitly declared, the Copy Constructor’s automatic generation will be disabled.
Move Constructor
If any one of the following member functions are explicitly defined, copy constructor, copy assignment operator, move assignment operator and destructor then the Move Constructor will not be automatically generated.
Copy Assignment Operator
If a destructor is explicitly declared, then the generation of the copy assignment constructor is deprecated (C++11 onwards). It is still generated (as of C++20), but it may be removed entirely in a future release.
Furthermore, if either the move constructor or move assignment operator are explicitly declared, the Copy Constructor’s automatic generation will be disabled.
Move Assignment Operator
If any one of the following are explicitly defined, copy constructor, copy assignment operator, move constructor and destructor, then the Move Assignment Operator will not be automatically generated.
This marks the end of the “C++ Special Member Functions” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section.