user8814100
user8814100

Reputation:

Static Object (Item 4 in Effective C++ 3rd Edition)

In item 4 (page 30/31 in Effective C++ 3rd Ed. ) the following Code example is provided:

Translation Unit 1:

class FileSystem {
public:
    ...
    std::size_t numDisks() const;
    ...
};

extern FileSystem tfs;

Translation Unit 2:

class Directory {
public:
    Directory( params );
    ...
};

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

Directory tempDir( params );

Now Meyers refers to tempDir as static object. (page 31, 2nd paragraph)

But tfs and tempDir were created by different people at different times in different source files — they’re non-local static objects defined in different translation units.

Well obviously tfs is static because it was declared with extern, but why the hack should tempDir be static?

Upvotes: 0

Views: 122

Answers (1)

Bo Persson
Bo Persson

Reputation: 92381

static has (too) many meanings in C++. Here it means that tempDir is defined in the static data area, outside of any functions.

It is not dynamically allocated and it is not a local automatic storage variable.

Upvotes: 4

Related Questions