(C++) Big Four

February 27, 2017 · View on GitHub

In C++98 the Big Four are the following four special class member functions [1]:

  1. Default constructor
  2. Copy constructor
  3. Copy assignment operator
  4. Destructor

In C++11 there are the Big Six.

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() { std::cout << "Destructor\n"; }
  Gossip& operator=(const Gossip&) { std::cout << "Copy 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
``` 

## [References](CppReferences.md)

1.  [Herb Sutter](CppHerbSutter.md), [Andrei
    Alexandrescu](CppAndreiAlexandrescu.md). C++ coding standards: 101
    rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6.
    Introduction 'Class Design and Inheritance'