Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) default constructor

 

'A default constructor is a constructor which needs no arguments.' [2]

 

struct MyClass
{
  MyClass() {}; //A default constructor
  //Rest of the class
};

 

struct MyClass
{
  MyClass(int x) : m_x(x) {}; //A default constructor
  //Rest of the public part of the class
  private:
  int m_x;
  //Rest of the private part of the class
};

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice, page 525: '[7] Give a class a default constructor if and only if there is a "natural" default value'
  2. Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. 4.3.14: 'A default constructor is a constructor which needs no arguments.'

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml