Namespaces

Using Namespaces

Section 13.4 in the programmer's guide to C++ describes how namespaces can be defined, but it does not clearly recommend a way of accessing them. This is now very important because of the introduction of the standard template library, which is in the std namespace. The following example used the std namespace, but other namespaces should be used in the same way. In a header file, use the :: operator to access a namespace like this:

// A header file called mystuff.h
#include <string>
...
class MyClass {
public:
   std::string address();
   void address( std:: string value )
   void dump();
   ...
private:
   std::vector<int> size;
   ...
};

Always use the :: operator in header files. Never use a using namespace statement globally in a header file because it will merge its specified namespace with the global namespace in any program that uses the header file. In source files, the :: operator is also the preferred way to access a namespace:

// a source code file
#include "mystuff.h"
...
std::string MyClass::address()
{
   std::string temp;
   ...
   return temp;
}

Alternatively, the scope prefix can be avoided by putting a using statement at the start of a function, like this:

#include "mystuff.h"
...
void MyClass::dump()
{
   using namespace std;
   cerr << Data dump follows << endl;
   cerr << ----------------- << endl
   ...
}

This introduces the specified namespace into the function's local namespace, which is sometimes convenient when a lot of names from a namespace are being used in a function. However, it is bad practice to put a using statement in the global scope of a source file because it pollutes all of the following function's namespaces.

Building User Defined Namespaces

User defined namespaces are strongly recommended. They simplify the choice and use of names in a program by preventing name clashes. They group related components, such as classes, global functions and constants into logical modules. They greatly assist the safe development of larger programs, which are composed of independent compilation units. Very few of the examples in the programmer's guide to C++ actually use namespaces. This was done to simplify the book's presentation and does not imply that namespaces should not be used in practice.

Building a user defined namespace is easy. Its names should be introduced to the namespace in header files:

// file: lottery.h

namespace Lottery {

  const int MAX = 30;

  class Card {
  public:
  Class();
  void generate();
  .....
  };

  void merge ( Card c1, Card c2 );
}

The source files that definition the above include the header files, and use the :: operator to associate with the namespace:

// file: lottery.cpp

#include "lottery.h"

Lottery::Card::generate()
{
  ....
}

void Lottery::merge ( Lottery::Card c1, Lottery::Card c2 )
{
  ....
}

When you use namespaces, it is worth noting that in at least one leading C++ developement environment the class browser is confused by namespaces.