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的更多相关文章

  1. 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) ...

  2. 条款4:确定对象被使用前已被初始化(Make sure that objects are initialized before they're used)

    其实 无论学何种语言 ,还是觉得要养成先声明后使用,先初始化再使用. 1.永远在使用对象之前先将其初始化. 内置类型: 必须手工完成. 内置类型以外的:使用构造函数完成.确保每一个构造函数都将对象的一 ...

  3. 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 ...

  4. [转][iOS]NSHash​Table & NSMap​Table

    NSSet and NSDictionary, along with NSArray are the workhorse collection classes of Foundation. Unlik ...

  5. C++ essentials 之 static 关键字

    extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...

  6. Effective C++ 之 Item 4:确定对象被使用前已先被初始化

    Effective C++ Chapter 1. 让自己习惯C++ (Accustoming Yourself to C++) Item 4. 确定对象被使用前已先被初始化 (Make sure th ...

  7. 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 ...

  8. 关于PKCS5Padding与PKCS7Padding的区别

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  9. PKCS5Padding与PKCS7Padding的区别

    工作中,我们常常会遇到跨语言平台的加密解密算法的交互使用,特别是一些标准的加解密算法,都设计到数据块Block与填充算法的问题,例如C#与JAVA中的常见的填充算法如下: .Net中的填充算法: 成员 ...

随机推荐

  1. R语言改变大小写 toupper()和 tolower()函数

    这些函数改变字符串的字符的大小写. 语法 toupper()和 tolower()函数的基本语法为: toupper(x) tolower(x) 以下是所使用的参数的说明: x - 向量输入. 示例 ...

  2. JS数组和函数 小记

    数组 JS中的数组来自window,是一个全局的对象,typeof的值是'object'. 创建数组: 1.Array(3):当只传一个值的时候,会生成一个长度为该数值的空数组. 2.Array(3, ...

  3. Spring----有关bean的配置

    1.单例类的配置如果我们想创建一个单例类的bean,只能会通过静态工厂来创建.下图为一个单例类: Stage并没有提供公开的构造方法,构造方法都是私有的,必须通过getInstance()方法获得已经 ...

  4. asp.netCore连接多个数据库

    1.首先要有对应的context实体类, 多个实体类的构造函数的参数都应该是集合 public class firstContext : DbContext { //多个数据库应该使用这个构造函数,参 ...

  5. Docker学习(三): Dockerfile指令介绍

    特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! =============系列文章============= 1 ...

  6. code.google.com certificate error: certificate is for www.google.com

    有时候我们会碰到下面错误:code.google.com certificate error: certificate is for www.google.com,类似如下: D:\>go ge ...

  7. docker 无法启动容器,run容器后状态为restarting

    问题:如题,无法进入容器,docker logs 容器id  发现 哦 ,原来缺少个文件,这些就容易了

  8. 数组的filter()方法

    filter()也是一个用的不多的方法,但有时候还是比较有用的: 首先,Array.filter()是数组的方法,它作为数组方法被调用,传入一个callback,返回Array中符合callback条 ...

  9. csharp:SQLite and Access using C# code read data

    SQLite sql script: CREATE TABLE BookKindList ( BookKindID INTEGER PRIMARY KEY AUTOINCREMENT, BookKin ...

  10. chrome devtools的debug相关

    搜索ctrl+p:搜索Sources面板中指定的文件:然后在主窗口文件标签右键选择reveal in navigator可以在目录中显示当前文件.ctrl+f:搜索devtool主显示窗口所在文件的指 ...