C++ Special Member Functions

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:

  1. Default Constructor
  2. Destructor
  3. Copy Constructor
  4. Move Constructor
  5. Copy Assignment Operator
  6. 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 constructorCopy constructorCopy operator=Move constructorMove operator=Destructor
NothingYESYESYESYESYESYES
Any constructorNOYESYESYESYESYES
Default constructorNOYESYESYESYESYES
Copy constructorNONOYESNONOYES
Copy operator=YESYESNONONOYES
Move constructorNODELETEDDELETEDNONOYES
Move operator=YESDELETEDDELETEDNONOYES
DestructorYESYESYESNONONO

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.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments