[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中的填充算法: 成员 ...
随机推荐
- iOS仿今日头条滑动导航
之前写了篇博客网易首页导航封装类.网易首页导航封装类优化,今天在前两个的基础上仿下今日头条. 1.网易首页导航封装类中主要解决了上面导航的ScrollView和下面的页面的ScrollView联动的问 ...
- RxJS库
介绍 RxJS是一个异步编程的库,同时它通过observable序列来实现基于事件的编程.它提供了一个核心的类型:Observable,几个辅助类型(Observer,Schedulers,Subje ...
- 最新的 Vue 相关开源项目库汇总
优秀的vue 开源后台管理开源系统框架 https://panjiachen.github.io/vue-element-admin-site/#/zh-cn/README UI组件 开发框架 实用库 ...
- [模板]选择排序&&冒泡排序&&插入排序
#include<iostream> #include<cstdio> #include<bits/stdc++.h> using namespace std; v ...
- Hadoop学习笔记(8) ——实战 做个倒排索引
Hadoop学习笔记(8) ——实战 做个倒排索引 倒排索引是文档检索系统中最常用数据结构.根据单词反过来查在文档中出现的频率,而不是根据文档来,所以称倒排索引(Inverted Index).结构如 ...
- Spring Boot学习笔记(二)全局捕获异常处理
非常简单只需要创建自己的异常处理类,加上两个注解,就可以了
- lincode 题目记录6
the Duplicate Number 132 PatternFind 找重复的数字··直接暴力枚举是不行的···又超时提示·· 暴力枚举的写法· res=0 def findDuplicate( ...
- 拆系数FFT(任意模数FFT)
拆系数FFT 对于任意模数 \(mod\) 设\(m=\sqrt {mod}\) 把多项式\(A(x)\)和\(B(x)\)的系数都拆成\(a\times m+b\)的形式,时\(a, b\)都小于\ ...
- 转:Redis和Memcache的区别分析
Redis和Memcache的区别分析 原文链接:http://blog.csdn.net/u013474436/article/details/48632665 简单区别: 1. Redis中,并 ...
- CSS 伪类(下)结构性伪类\UI伪类\动态伪类和其他伪类 valid check enable child required link visit
伪类选择器汇总伪类选择器有4种, 结构性伪类\UI伪类\动态伪类和其他伪类. 具体如下 结构性伪类选择器结构性伪类选择器它能够根据元素在文档中的位置选择元素, 这类元素都有个前缀":&q ...