Use global names only for classes

Ideally, only classes should have names with global scope (that is, not nested within a class). For this reason, avoid globally scoped functions, enumeration types, or constants. Make functions static members of some class, and define enums and constants within a class. It's even possible to nest classes inside other classes, if they don't need global scope. There are only two general exceptions; (at Taligent, an architect must approve all others):

By keeping the global name space uncluttered, you reduce name collisions and make it easier to figure out where a name is coming from. C++ helps by allowing declarations in class scope, allowing static members, and providing qualification to access identifiers declared inside classes.

      class TFoo {
      public:
          enum EWho {kFred, kBarney};
      ...
      };
      
      TFoo::EWho person = TFoo::kFred;
This lets you put constants
associated with different classes into different name spaces, similar to when C changed a few years back so that structure members from different struct's were in different name spaces.

NOTE All nested declarations appear in the class' name space, even the enumeration type. Because class declarations can be nested, scopes can nest to multiple levels and require multiple levels of qualification. Within the body of a class, however, names declared in its scope don't require qualification.

To avoid name collisions, use static members to put ordinary functions and global variables into the scope of their associated class.

      
      class TView {
      public:
    static void Initialize(); static const TText kMagicWord; static const long kMagicNumber; ... }; TView::Initialize(); ...TView::kMagicWord... i = TView::kMagicNumber;

Avoid ordinary globals

Most global functions and variables should be static members of some class. The same applies to constants--make them members of an enumeration inside a class, if possible. Global variables that aren't constants of the sort illustrated in the previous example shouldn't be public at all; instead, access them through member functions, static or normal:

      class TFoo {
      public:
          static Boolean fgSomeFlag;      // BAD!
      }
      
      TFoo::fgSomeFlag = TRUE;            // BAD!
The C++ namespace feature allows the same kind of scoping control for global names. A namespace
construct acts like a class definition by providing a name scope. Unlike class declarations, declarations within a name space needn't be contiguous--for example, declarations in the same name space can be in different header files. Also, there is a using construct that imports names from a name space into the local scope. The Taligent Application Environment might use the namespace feature; however, it is better to move names into class scope.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker