Effective C++ Item 4 Make sure that objects are initialized before there are used

1. For most types(non-built-in types), a single call to copy constructor is more efficient - sometimes much more efficient than a call to default construtor followed by a call to copy assignment constructor.

2. Base class is initialized before derived class and within a class, data members are initialized in the order that they declared. You need to expicitly initialze every member variable in class.

3. There is one more thing you need to worry about, the order of initialize non-local static variable. Static variable is the one that exists from the time that it is constructed until the end of the program. Static variable exists until the its destructor are called when main function finish executing. Local static refer to those declare inside a function, and the other static variable are all called non-local static variables. 

Translation Unit(编译单元) is the source code give rise to single object file. It‘s basically a single source file, plus all of its #include files.

 

4. Now let‘s talk about a real problem about initialization of non-local static objects. Imaine at least two seperated source files, each of which contains at least one non-local static object. If intialization of one non-local static object in one translation unit use a non-local static objects in a different translation unit, the object it uses could be uninitialized, because the relative order of non-local static objects defined in different translation units is undefined.

 

Let‘s see an example:

In FileSystem.h, you define

class FileSystem {
public:
    ...
    std::size_t numDisk() const;
    ...
};
 
extern FileSystem tfs;

and suppose that someone use your code like this

class Directory {
public:
    Directory(params);
    ...
};
 
Directory:Directory(params) {
    ...
    std::size_t = tfs.numDisk();
    ...
}

If he going to define a Directory object

Directory tempDir(params);

Unless tfs is initialized before tempDir, tempDir will attempted to use FileSystem before it is initialized.

 

One solution is to make a small design change. All that has to do is to move each non-local static object into its own function, where it‘s declared static. These function returns the reference it contains. Then call the function instead of refering to the object. In other word, non-local static object is replaced by local static objects. C++ guarantee that local static objects are initialized when the object‘s definition is first encountered during a call to that function. Here‘s the technique to both the FileSystem and tempDir:

FileSystem& tfs() {
    static FileSystem fs;
    return fs;
};
 
class Directory {
public:
    Directory(params);
    ...
};
 
Directory:Directory(params) {
    ...
    std::size_t = tfs().numDisk();
    ...
}

The reference-returning function dictated by this scheme are always simple: define and return a local static object on line 1 and return it on line 2. This simplicity makes them excellent candidates for inlining, especially if they‘re called frequently.

Effective C++ Item 4 Make sure that objects are initialized before there are used,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。