class TFoo { public: enum EWho {kFred, kBarney}; ... }; TFoo::EWho person = TFoo::kFred;
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.
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:
The C++ class TFoo {
public:
static Boolean fgSomeFlag; // BAD!
}
TFoo::fgSomeFlag = TRUE; // BAD!
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.
Generated with WebMaker