(C++) Big Six
February 27, 2017 · View on GitHub
Since C++11 the Big Six are the following six special class member functions:
- Default constructor
- Copy constructor
- Move constructor
- Copy assignment operator
- Move assignment operator
- Destructor
In C++98 there were only four, called the Big Four.
Example
#include <iostream>
///Gossip is a class that tells you what is happening to it
struct Gossip
{
Gossip() { std::cout << "Default constructor\n"; }
Gossip(const Gossip&) { std::cout << "Copy constructor\n"; }
Gossip(const Gossip&&) { std::cout << "Move constructor\n"; }
~Gossip() { std::cout << "Destructor\n"; }
Gossip& operator=(const Gossip&) { std::cout << "Copy assignment operator\n"; }
Gossip& operator=(const Gossip&&) { std::cout << "Move assignment operator\n"; }
};
int main()
{
{
Gossip g; //Default constructor
const Gossip h(g); //Copy constructor
g = h; //Copy assignment operator
//Destructor of g and h
}
}
Screen output:
Default constructor
Copy constructor
Copy assignment operator
Destructor
Destructor
```
## Advice
- Regarding the six operations above, implement as little as possible and declare as much as possible. Any operation not
implemented shall be declared as default or delete [1]
## [References](CppReferences.md)
1. Gottschling, Peter. Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers. Addison-Wesley Professional, 2015.
Chapter 2.5: 'Regarding the six operations above, implement as little as possible and declare as much as possible. Any operation not
implemented shall be declared as default or delete'