I read effective C++ which item 04 mentioned
Avoid initialization order problems across translation units by re-placing non-local static objects with local static objects.
I think that "global and only-one object" should be singleton pattern, not extern object after I read this item.
such as I/O object(std::cout)
But std::cout seems extern object. (http://www.cplusplus.com/reference/iostream/cout/)
I am confused about this.
edit: add code
I Capture some code form this book.
First is bad code:
class FileSystem {
// from your library’s header file
public:
...std::size_t numDisks( ) const;
// one of many member functions...
};
extern FileSystem tfs;
the relative order of initialization of non-local staticobjects defined in different translation units is undefined.
So abovementioned code may be fault when I call tfs.
Because tfs may not complete initialization.
The recommand code:
class FileSystem { ... }; // as before
FileSystem& tfs()
{
static FileSystem fs;
return fs;
}
class Directory { ... };// as beforeDirectory::Directory( params )
Directory::Directory( params ) // as before, except references to tfs are
//now to tfs( )
{
...
std::size_t disks = tfs().numDisks( );
...
}
Directory& tempDir()
{
static Directory td(params);
return td;
}