《Effective C++》读书笔记之四 Item 4. Make sure that objects are initialized before they're used

本文主要是介绍《Effective C++》读书笔记之四 Item 4. Make sure that objects are initialized before they're used,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

     这篇Item主要讲的是记得在使用之前初始化对象的成员变量,并介绍了几种初始化的方法:
     1. Manually initialize objects of built- in type, because C++ only sometimes initializes them itself.

2. In a constructor, prefer use of the  member initialization list to assig nment inside the body of the constructor. List data members in the initialization list in the same order they're declared in the class.
This constructor yields the same end result as the one above, but it will often be more efficient. The assignment- based version first called default constructors to initialize theName, theAddress, an d thePhones, then promptly assigned new values on top of the default- constructed ones. All the work performed in those default constructions was therefore wasted. The member initialization list approach avoids that problem, because
the arguments in the initialization list are used as constructor arguments for the various data members.

3. Avoid initialization order problems across translation units by replacing non - local static objects with local static objects.
Base classes are initialized before derived classes (see also Item 12(See 10.8)), and within a class, data members are initialized in the order in which they are declared. 
To avoid reader confusion, as well as the possibility of some truly obscure behavioral bugs, always list members   in the initialization list in the same order as they're declared in the class.
A static object  is one that exists from the time it's constructed until the end of the program. Stack and heap- based objects are thus excluded. Included are global objects, objects defined at namespace scope, objects declared static  inside classes, objects declared static  inside functions, and objects declared  static at file scope. Static objects inside functions are known as  local static objects (because they're local to a function), and the other kinds of static objects are known as   non- local static objects. 
This approach is founded on C++'s guarantee that local static objects are initialized when the object's definition is first encountered during a call to that function.

这篇关于《Effective C++》读书笔记之四 Item 4. Make sure that objects are initialized before they're used的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/758316

相关文章

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa