class FileSystem {
...
int numDisks();
...
};
FileSystem& theFileSystem() // this replaces the theFileSystem object
{
static FileSystem fileSystem; // define and initialize a local static object
return fileSystem;
}
class Directory {...};
Directory::Directory()
{
...
std::size_t disks = FileSystem::theFileSystem().numDisks();
...
}
The book asks us to replace non-local static variable with local static variable, but when we call FileSystem::theFileSystem().numDisks()
multiple times, it will declare static FileSystem fileSystem
multiple time which should not be good, right?