To initialize an object is to give it an initial value.
There are multiple types of initialize:
There are four initialization styles, prefer the first (called list initialization) [1].
- T a { b };
- T a = { b };
- T a = b;
- T a(b);
- Prefer {} initialization over = and () initialization [1]
- Initialize members and bases in their order of declaration [2]
- Although it is not always necessary to initialize every variable explicitly, doing so will help you avoid many kinds of problems [3]
- initialize example 1: a difference between {} and = with references
- initialize example 2: initializing a reference member variable
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice, page 525: '[6] Prefer {} initialization over = and () initialization'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 17.7. Advice, page 525: '[9] Initialize members and bases in their order of declaration'
- Paul Deitel, Harvey Deitel. C++11 for progrgrammers (2nd edition). 2014. ISBN: 978-0-13-343985-4. Chapter 2.4, Error Prevention Tip 2.1. page 25: 'Although it is not always necessary to initialize every variable explicitly, doing so will help you avoid many kinds of problems.'