[EffectiveC++]item04:Make sure the objects are initialized before they're used
28 页
C++规定,对象的成员变量的初始化动作发生在进入构造函数本体之前。
构造函数的一个较佳的写法是,使用所谓的member initialization list替换赋值动作。
29页
但请立下一个规则,规定总是在初值列中列出所有成员变量,以免还得记住哪些成员变量可以无需初值。
31页
幸运的是一个小小的设计便可以完全消除这个问题。将每个non-local static对象搬到自己的专属函数内(改对象在此函数内被声明为static)。这些函数返回一个reference指向它所含的对象。然后用户调用这些函数,而不直接指涉这些对象。
换句话说,non-local static对象被local static对象替换了。Design Patterns fans想必认出来了,这个是Singleton模式的一个常见实现手法
1)为内置对象进行手工初始化,因为c++不保证初始化她们
2)构造函数最好使用member initialization list,而不要在构造函数本体内使用assignment。list列出的成员变量,起排列次序应该和他们在class中的声明次序相同。(不同的话也是合法的)
3)为避免“跨编译单元之初初始化次序”问题,请以local static对象替换non-local static对象。
#include <iostream>
#include <string.h>
using namespace std;
class PhoneNumber
{
public:
PhoneNumber(){cout << "in PhoneNumber\n" ;}
};
class ABEntry
{
public:
ABEntry(){cout << "in ABEntry()\n" ;};
ABEntry(const PhoneNumber & phone);
private:
PhoneNumber PN;
};
ABEntry::ABEntry(const PhoneNumber & phone)
{
cout << "in ABEntry(const ...)\n" ;
PN = phone;
}
int main()
{
PhoneNumber pn;
cout << "in main 2\n" ;
ABEntry ab;
cout << "in main 3\n" ;
ABEntry ab2(pn);
return 0;
}
/work/ctest/effectivec++$ g++ 4_2.cpp -o 1 && ./1
in PhoneNumber
in main 2
in PhoneNumber
in ABEntry()
in main 3
in PhoneNumber
in ABEntry(const ...)
//甚至当你想要default构造一个成员变量,你都可以使用成员初值列,只要指定无物(nothing)作为初始化实参即可。
class PhoneNumber
{
public:
PhoneNumber(){cout << "in PhoneNumber\n" ;}
};
class ABEntry
{
public:
ABEntry():PN(){cout << "in ABEntry()\n" ;};
ABEntry(const PhoneNumber & phone);
private:
PhoneNumber PN;
};
ABEntry::ABEntry(const PhoneNumber & phone):PN(phone)
{
cout << "in ABEntry(const ...)\n" ;
PN = phone;
}
int main()
{
PhoneNumber pn;
cout << "in main 2\n" ;
ABEntry ab;
cout << "in main 3\n" ;
ABEntry ab2(pn);
return 0;
}
in PhoneNumber
in main 2
in PhoneNumber
in ABEntry()
in main 3
in ABEntry(const ...)
2)

[EffectiveC++]item04:Make sure the objects are initialized before they're used的更多相关文章
- effective c++ 条款4 make sure that objects are initialized before they are used
1 c++ 类的数据成员的初始化发生在构造函数前 class InitialData { public: int data1; int data2; InitialData(int a, int b) ...
- 条款4:确定对象被使用前已被初始化(Make sure that objects are initialized before they're used)
其实 无论学何种语言 ,还是觉得要养成先声明后使用,先初始化再使用. 1.永远在使用对象之前先将其初始化. 内置类型: 必须手工完成. 内置类型以外的:使用构造函数完成.确保每一个构造函数都将对象的一 ...
- Injecting and Binding Objects to Spring MVC Controllers--转
I have written two previous posts on how to inject custom, non Spring specific objects into the requ ...
- [转][iOS]NSHashTable & NSMapTable
NSSet and NSDictionary, along with NSArray are the workhorse collection classes of Foundation. Unlik ...
- C++ essentials 之 static 关键字
extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...
- Effective C++ 之 Item 4:确定对象被使用前已先被初始化
Effective C++ Chapter 1. 让自己习惯C++ (Accustoming Yourself to C++) Item 4. 确定对象被使用前已先被初始化 (Make sure th ...
- Core Java Volume I — 3.10. Arrays
3.10. ArraysAn array is a data structure that stores a collection of values of the same type. You ac ...
- 关于PKCS5Padding与PKCS7Padding的区别
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- PKCS5Padding与PKCS7Padding的区别
工作中,我们常常会遇到跨语言平台的加密解密算法的交互使用,特别是一些标准的加解密算法,都设计到数据块Block与填充算法的问题,例如C#与JAVA中的常见的填充算法如下: .Net中的填充算法: 成员 ...
随机推荐
- Node.js之Console用法小结
/** * Created by city--online on 16/3/9. */ //console.time()和console.timeEnd()输出中间代码的执行时间(注意:time和ti ...
- 问题集录--新手入门深度学习,选择TensorFlow 好吗?
新手入门深度学习,选择 TensorFlow 有哪些益处? 佟达:首先,对于新手来说,TensorFlow的环境配置包装得真心非常好.相较之下,安装Caffe要痛苦的多,如果还要再CUDA环境下配合O ...
- nuxt.js踩坑之 - SSR 与 CSR 显示不一致问题
[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This ...
- 计算细胞数【BFS】
问题描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数. 输入格式 2行:第1行为两个整数 mm, nn, 代表矩阵 ...
- linux的日常经常使用的命令
现在经常用到linux命令,又时候回忘记,我就做个小笔记,大家也可以补充补充.....可以评论一下,我会截图做笔记的 netstat -ntlp //查看当前系统进程和端口等信息 tail -f fi ...
- spring 事务管理机制
1. spring 事务管理抽象 spring 的事务策略机制的核心就是 org.springframework.transaction.PlatformTransactionManager 接口. ...
- JSP简单实现登录和注销
JSP简单实现登录和注销 需求:用户登录成功后跳转到欢迎页面 用户登录失败跳转到初始的登录界面 用户点击注销,用户退出登录状态需要重新登录 登录页面的JSP代码: <%@ page langua ...
- safari
http://www.zhangxinxu.com/wordpress/2014/10/mobilebone-js-mobile-web-app-core/ http://rawgit.com/zha ...
- FCC的javascript初级算法题解答
FCC上的javascript基础算法题 前一阵子做的基础算法题,感觉做完后收获还蛮大的,现在将自己的做法总结出来,供大家参考讨论.基本上做到尽量简短有效,但有些算法还可以继续简化,比如第七题若采用正 ...
- drupal7 获取当前使用的主题的名称
直接引用全局变量就行: 参考: 代码测试: global $theme, $theme_key; echo $theme; echo '<br>'; echo $theme_key; 结果 ...