Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
The constructor is the class element called when a class is created. The class element that is called when a class goes out of scope is called the destructor.
#include <iostream> |
The code above shows a default constructor: a constructor that has no arguments or only default arguments. Constructors can have multiple arguments. Additionally, a class can have multiple constructors.
Avoid calling virtual member functions in constructors and destructors [2].

Constructor initialization
Member variables can be initialized in the constructor body, as shown in the example below:
struct MyClass |
Prefer initialization to assignment in constructors [1], as shown in the example below:
struct MyClass |
Note that the example above, with a const member variable cannot be constructed without the proper constructor initialization shown.
delegation
Delegation is the technique of constructors calling each other:
struct MyClass |
See the page about delegation for more information.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.