12.18

Effective C++ Scott Meyers  

Chapter 1. Uses and Abuses of vector

1. Item 4: Make sure that objects are initialized before they're used.

  Use member initialization list to initialize the member.

  Sometimes, the initialization list must be used, even for built-in types. For example, data members that are const or are references must be initialized.

  Always list members in the initialization list in the same order as they're declared in the class.

  When class has multiple constructors, omit the entries in the list for data members where assignment works as well as true initialization, moving the assignments to a single function that all the constructors call.

  Static objects declared inside functions are known as local static objects, and the other kinds of static objects are known as non-local static objects.

  The relative order of initialization of non-local static objects defined in different translation units is undefined.

  Define the non-local static objects in local functions, where it's declared static. These functions return references to the objects they contain. (Singleton Pattern)

  The functions contain static objects makes them problematic in multithreaded systems. One way to deal with it is to manually invoke all the reference-returning functions during the single-threaded startup portion of the program. This eliminates initialization-related race conditions.

posted @ 2018-12-24 12:08  lefthook  阅读(112)  评论(0编辑  收藏  举报