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. Debian9安装桌面环境

    更新安装源 apt-get update 安装 x-window apt-get install x-window-system-core 安装 gnomeapt-get install gnome- ...

  2. 图像RGB2YUV与YUV2RGB格式互转介绍

    1 YUV格式与RGB格式说明 由于不同国家的电视信号系统支持的图像格式不同,有YUV格式成像,也有RGB格式成像,因此为了保证兼容性,需要进行RGB与YUV格式的互转. 另外YUV格式具有亮度信息和 ...

  3. 反射 XMLUtil

    package com.dys.util; import java.beans.Introspector; import java.beans.PropertyDescriptor; import j ...

  4. 【angular5项目积累总结】消息订阅服务

    code import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable ...

  5. 【C#高级】泛型(一)

    泛型,.net 2.0之后出现,基本只要代码中出现 ‘<>’ 尖括号就可以确定是泛型. 在2.0之前大多是使用Object来代替,因为所有类都是Object的派生类,根据继承的原理Obje ...

  6. 获取用户Ip地址通用方法

    1 public static function getIp() 2 { 3 if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) 4 { ...

  7. golang命令和VSCode配置

    Go是一门全新的静态类型开发语言,具有自动垃圾回收.丰富的内置类型.函数多返回值.错误处理.匿名函数.并发编程.反射等特性 golang常用命令: go env #查看go的环境 echo %GORO ...

  8. GridFS使用及配合nginx实现文件服务

    Mongodb下GridFS使用及配合nginx实现文件服务 一.GridFS简介 GridFS是mongodb下用来存储文件的一种规范,所有官方支持的驱动均实现了GridFS规范. Mongodb本 ...

  9. C#中区别多态、重载、重写

    重写是指重写基类的方法,在基类中的方法必须有修饰符virtual,而在子类的方法中必须指明override. 格式: 基类中: public virtual void myMethod() { } 子 ...

  10. springmvc 框架原理

    先来个原理图,镇博. (图片出处:http://www.cnblogs.com/selene/p/4658554.html,感谢博主的图) 着重看:处理器映射器,处理器适配器,这两个的配置. 这两个的 ...