Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Big Four

 

The Big Four are the following four special class member functions [1]:

 

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

 

 

 

 

 

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

 

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

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict