effective c++(04)之对象使用前初始化

对于内置类型以外的初始化责任落在构造函数身上。如下:

class PhoneNumber{};
class ABEntry{
public:
    ABEntry( const string& name, const string& address, const list<PhoneNumber>& phones );
private:
    string theName;
    string theAddress;
    list<PhoneNumber> thePhones;
    int numTimesConsulted;
};

ABEntry::ABEntry( const string& name, const string& address, const list<PhoneNumber>& phones )
{
    theName = name;
    theAddress = address;
    thePhones = phones;
    numTimesConsulted = 0;
}

以上对数据成员不是初始化,而是赋值。初始化发生在更早的时候,发生于这些成员的default构造函数被自动调用之时。

ABEntry::ABEntry( const string& name, const string& address, const list<PhoneNumber>& phones )
:  theName(name),
theAddress(address), thePhones(phones), numTimesConsulted(
0)
{}

以上这个通常效率要高,这次只调用了一次默认构造函数而没有使用赋值。

规则:总是在初值列中列出所有的成员变量,以免还要记住那些成员变量可以无需初值。

如果成员变量是const或者references,他们就一定需要初值而不能被赋值。最简单做法:总是使用成员初值列。

成员初始化次序:base classes更早于derived classes被初始化。class成员总是以其声明的次序被初始化(即privated中的次序)。

 

class FileSystem{
public:
    size_t numDisks() const;
};
extern FileSystem tfs;

class Directory{
    Directory( params);
};
Directory::Directory(params)
{
    size_t disks = tfs.numDisks();
}
Directory tempDir(params);

 

以上FileSystem和Directory属于不同的文件系统,tempDir在使用之前需要将tfs初始化。但是系统并不能保证其初始化,如何将tfs这个non-local static对象在被disks使用前进行初始化。方法:将每个non-local static对象搬到自己的专属的函数里面,返回一个reference指向所包含的对象。使得non-local static对象被local static对象所替代。解决方案如下:

 

总结

1. 为内置类型对象进行手工初始化,因为c++不一定保证初始化他们;

2. 构造函数最好使用成员初值列,而不要在构造函数本体内使用赋值操作。初值列列出的成员变量,排列次序应该和声明次序相同;

3. 为免除“跨编译单元制初始化次序”问题,以local static对象替换non-local static对象。

posted @ 2013-07-10 11:46  2011winseu  阅读(677)  评论(0编辑  收藏  举报