[ Team LiB ] Previous Section Next Section

Gotcha #22: Static and Extern Types

No such thing. However, experienced C++ programmers sometimes lead inexperienced ones astray with declarations that appear to apply a linkage-specifier to a type (see Gotcha #11):



static class Repository { 


   // . . .


} repository; // static


Repository backUp;  // not static


Even though types may have linkage, linkage-specifiers always refer to an object or function, not a type. It's better to be clear:



class Repository { 


  // . . .


};


static Repository repository;


static Repository backUp;


Note also that use of an anonymous namespace may be preferable to use of the static linkage-specifier:



namespace { 


Repository repository;


Repository backUp;


}


The names repository and backUp now have external linkage and can therefore be used for a wider variety of purposes than a static name can (for instance, in a template instantiation). However, like statics, they're not accessible outside the current translation unit.

    [ Team LiB ] Previous Section Next Section