Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) initialize

 

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].

 

  1. T a { b };
  2. T a = { b };
  3. T a = b;
  4. T a(b);

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. 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'
  2. 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'
  3. 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.'

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict