Back: Variable Scoping in For Loops
Forward: The explicit Keyword
 
FastBack: The explicit Keyword
Up: Changeable C++
FastForward: Compiler Quirks
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

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 identical 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;



This document was generated by Gary V. Vaughan on February, 8 2006 using texi2html