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.
'A constructor is a function which initializes an object.' [18]
There are multiple types of constructors:
A class can have multiple constructors. Constructors can have zero to multiple arguments. Delegation is the technique of constructors calling each other. If a class does not have a constructor, it can be initialized by memberwise initialization [13].
- Design constructors, assignments, and the destructor as a matched set of operations [10]
- Define a constructor to handle initialization of objects [8]
- Let a constructor establish an invariant [6,11], and throw if it cannot [6]
- Ensure that an object is fully initialized before the client code invokes the object's member functions [19]
- By default declare single-argument constructors explicit [9]
- Be sure that every resource acquired in a constructor is released when throwing an exception in that constructor [7]
- Avoid calling virtual member functions in constructors and destructors [2]
- Don't call virtual member functions in constructors and destructors [17]
- Prefer initialization to assignment in
constructors [1]. In C++98, use
the
T(e)
notation for construction [1]. In C++11, use theT{e}
notation for construction [5] - If a constructor acquires a resource, its class needs a destructor to release the resource [12]
- Give a class a default constructor if and only if there is a "natural" default value [15, 20]
- If a class is a container, give it an initializer-list constructor [14]
- An abstract class typically doesn't need a constructor [16]
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Item 48: 'Prefer initialization to assignment in constructors'
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Item 49: 'Avoid calling virtual functions in constructors and destructors'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Page 19, 1.3.2 'Advice', item 1: 'Use constructors to establish invariants'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Page 19, 1.3.2 'Advice', item 2: 'Use constructor/destructor pairs to simplify resource management'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 11.6. Advice. page 303: '[11] Use the T{e} notation for contruction'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 13.7. Advice. page 387: '[11] Let a constructor establish an invariant, and throw if it cannot'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 13.7. Advice. page 387: '[13] Be sure that every resource acquired in a constructor is released when throwing an exception in that constructor'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 16.4. Advice. page 479: '[4] Define a constructor to handle initialization of objects'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 16.4. Advice. page 479: '[5] By default declare single-argument constructors explicit'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[1] Design constructors, assignments, and the destructor as a matched set of operations'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[2] Use a constructor to establish an invariant for a class'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[3] If a constructor acquires a resource, its class needs a destructor to release the resource'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[5] If a class does not have a constructor, it can be initialized by memberwise initialization'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice. page 525: '[8] If a class is a container, give it an initializer-list constructor'
- 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'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 20.7. Advice. page 611: '[9] An abstract class typically doesn't need a constructor'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 22.7. Advice. page 663: '[7] Don't call virtual functions during construction and destruction'
- Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. 4.3.10: 'A constructor is a function which initializes an object.'
- Paul Deitel, Harvey Deitel. C++11 for programmers (2nd edition). 2014. ISBN: 978-0-13-343985-4. Chapter 3.5, Software Engineering Observation 3.2. page 54: 'Data members can be initialized in a constructor, or their values may be set later after the object is created. However, it's a good software engineering practice to ensure that an object is fully initialized before the client code invokes the object's member functions. You should not rely on the client code to ensure that an object gets initialized properly.'
- Gottschling, Peter. Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers. Addison-Wesley Professional, 2015. Chapter 2.3.1.1: 'Define a default constructor whenever possible'