首页 > 其他 > 详细

12.18

时间:2018-12-24 12:27:56      阅读:171      评论:0      收藏:0      [点我收藏+]

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.

12.18

原文:https://www.cnblogs.com/lefthook/p/10167913.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!