1.条款04:确定对象被使用前已先被初始化
这是一个比较好理解的条款,从刚学习C语言开始,这样的问题就一直伴随至今。
1.1 内置类型的初始化
对于int
、double
这样的内置类型,需要手动初始化,比如说:
int x= 0;
const char* text="A C-style string";
double d;
cin >> d;//以输入流的方式初始化
未初始化变量的错误,已经很少犯了,毕竟已经身经百战。
1.2构造函数初始化
这里纠正了我以前的误解,那就是但凡是在构造函数中做的事情就是初始化,然而,书中指出,带参数的构造函数使用成员初值列表
的做法可谓之初始化,初始化的顺序则是以成员初值列中的次序为标准(即使声明顺序不一样),而在函数体中的赋值操作,只能算作赋值:
class A
{
private:
int a;
public:
A(int value):a(value){}//这是初始化
};
//***************
class A
{
private:
int a;
public:
A(int value){a=value;}//这是赋值
};
其实两者效果没差,不过初始化的结果通常是效率更高。
对于默认构造函数来说,依然是使用成员初值列表的方式,让需要初始化的成员去调用各自的默认构造函数,例如:
class B
{
private:
int b;
public:
B():b(0){}
};
class A
{
private:
string t1;
B b;
int x;
public:
//各自调用各自的默认构造函数
A() :t1(), b(), x(0){}
};
1.3non-local static对象
什么是non-local static对象
static关键字的作用,在cppreference.com写道:
(参见:C++ KeyWords:static)
1.static storage duration with internal linkage specifier(静态存储周期声明)
2.declarations of class members not bound to specific instances(对象无关的成员)
函数内部的static对象被称作local static对象,反之,其他的static对象则是non-local static对象了。要成为static对象的方法有很多,static关键字是其特性的说明之一,正如上述1.所说。
书中描述的static对象,并不是我们常说的什么静态局部变量,静态全局变量什么的,前者指的是其存储周期,其寿命从被构造出来到程序结束为止,后者与变量的存储位置,作用范围有关。比如书中的例子:
extern FileSystem tfs;
是一个非静态全局变量,但是是一个non-local static 对象。
编译单元和示例分析
简单来说,cpp文件和其包含头文件就是一个编译单元。
现在的问题是,如果两个不同的编译单元分别包含两个non-local static对象,并且其中一个对象的初始化依赖于另外一个,则这两个对象的初始化顺序是未知的。
举个例子:
//first.cpp
#include <iostream>
#include "common.h"
test1 t1;
//second.cpp
#include <iostream>
#include "common.h"
test2 t2;
//last.cpp
#include <iostream>
#include "common.h"
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
//common.h
#ifndef _LIB_H_
#define _LIB_H_
#include <iostream>
using namespace std;
class test2;
extern test2 t2;
class test2
{
public:
test2()
{
value = 100;
cout << "test2 construct" << endl;
}
void said()
{
cout << "I am test2 " << "value=" << value << endl;
}
private:
int value;
};
class test1
{
public:
test1()
{
cout << "test1 construct" << endl;
t2.said();
}
};
#endif
示例中可以看到,test1实例的构造的前提是t2已经构造。first.cpp和second.cpp则是分别对于t1和t2的构造。t1和t2分别是不同编译单元中的non-local static对象。
#g++ -c -o first.o first.cpp
#g++ -c -o second.o second.cpp
#g++ -c -o last.o last.cpp
#g++ last.o first.o second.o
#./a.out
test1 construct
I am test2 value=0
test2 construct
Hello World
#g++ last.o second.o first.o
#./a.out
test2 construct
test1 construct
I am test2 value=100
Hello World
如果t2后于t1初始化,那么结果将是未定义的。
一个简单设计将可以消除这个问题:将non-local static对象放到其专属的函数内,该对象在函数内部也被声明为static,这些函数返回该对象的引用,这就是C++ 单例模式的一种常见手法。
因此,对上述示例进行修改,将non-local static对象放到函数内部,并返回一个对象引用:
//second.cpp
test2& getInstance()
{
static test2 t2;
return t2;
}
此时,我们需要t2对象的时候,使用其专属的函数,而不是原来的non-local static对象:
class test1
{
public:
test1()
{
cout << "test1 construct" << endl;
getInstance().said();
// t2.said();//取代了直接操作对象
}
};
这样就提供了初始化顺序的保证,因为对于local static对象来说,这只在函数调用的时候才会初始化,并且当没有初始t1的时候,t2也不会初始化,因而不会引发构造和析构的成本。
2.参考
1.Effective C++
2.http://blog.youkuaiyun.com/zhangyifei216/article/details/50549703
3.http://www.cnblogs.com/burandanxin/archive/2009/10/16/1584735.html
3.后记
EffectiveC++这本书没有多厚,但看了两天下来发现里面的知识点还是很多,原计划的是一篇博客弄懂2-3个条款,看样子现在不得不视情况而定了,呵呵,加油吧~