16.2.5 Namespaces
C++ namespaces are a facility for expressing a relationship between a
set of related declarations such as a set of constants. Namespaces also
assist in constraining names so that they will not collide with
other idential names in a program. Namespaces were introduced to the
language in 1993 and some early compilers were known to have incorrectly
implemented namespaces. Here's a small example of namespace usage:
| namespace Animals {
class Bird {
public:
fly (); {} // fly, my fine feathered friend!
};
};
// Instantiate a bird.
Animals::Bird b;
|
For compilers which do not correctly support namespaces it is possible
to achieve a similar effect by placing related declarations into an
enveloping structure. Note that this utilises the fact that C++
structure members have public protection by default:
|
struct Animals {
class Bird {
public:
fly (); {} // fly, my find feathered friend!
};
protected
// Prohibit construction.
Animals ();
};
// Instantiate a bird.
Animals::Bird b;
|
|