Namespace

Back Up Next

Home
The Secure Shell
FREE compiler
Namespace
Sample Assignment

Namespaces

Namespaces allows us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.

The form to use namespaces is:

namespace identifier
{
  namespace-body
}

Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:

 

namespace general

{

  int a, b;

}

In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator ::. For example, to access the previous variables we would put:

general::a
general::b

The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error. For example:

// namespaces

#include <iostream.h>

 

namespace first

{

  int var = 5;

}

 

namespace second

{

  double var = 3.1416;

}

 

int main () {

  cout << first::var << endl;

  cout << second::var << endl;

  return 0;

}

5
3.1416

In this case two global variables with the var name exist, one defined within namespace first and another one in second. No redefinition errors thanks to namespaces.

using namespace

The using directive followed by namespace serves to associate the present nesting level with a certain namespace so that the objects and functions of that namespace can be accesible directly as if they were defined in the global scope. Its utilization follows this prototype:

using namespace identifier;


Thus, for example:

// using namespace example

#include <iostream.h>

 

namespace first

{

  int var = 5;

}

 

namespace second

{

  double var = 3.1416;

}

 

int main () {

  using namespace second;

  cout << var << endl;

  cout << (var*2) << endl;

  return 0;

}

3.1416
6.2832

In this case we have been able to use var without having to precede it with any scope operator.


It is necessary to consider that the sentence using namespace has validity only in the block in which it is declared (understanding as a block the group of instructions withing key hooks {}) or in all the code if it is used in the global scope. Thus, for example, if we had intention to use first the objects of a namespace and then those of another one we could do something similar to:

// using namespace example

#include <iostream.h>

 

namespace first

{

  int var = 5;

}

 

namespace second

{

  double var = 3.1416;

}

 

int main () {

  {

    using namespace first;

    cout << var << endl;

  }

  {

    using namespace second;

    cout << var << endl;

  }

  return 0;

}

3.1416
6.2832

alias definition

We have the possibility of generating alternative names for namespaces that already exist, the form to do it is:

namespace new_name = current_name ;

Namespace std

One of the best examples that we can find about namespaces is the standard C++ library itself. According to ANSI C++ definition all the classes, objects and functions of the standard C++ library are defined within namespace std.

But this rule is almost as recent as the ANSI standard itself (1997) and many older compilers do not follow this rule. So, in order to differentiate the inclusion of these header files with members under namespace std from the older ones the standard has defined a new way to include the standard library header files:

#include <iostream>

That is a bit different from the traditional way that has been used during this tutorial:

#include <iostream.h>

Apparently the only difference is that we have included the header file without the .h extension. Nevertheless, if we include the library this way all the classes, objects and functions of it will no longer be directly in the global scope, but under the namespace std, and therefore to use them we will have to use either the scope operator :: or to include a using namespace directive. For example:

// pure ANSI-C++ hello world

#include <iostream>

 

int main () {

  std::cout << "Hello world in ANSI-C++\n";

  return 0;

}

Hello world in ANSI-C++

Although it is more usual to use using namespace and save us to have to use the scope operator :: before all the references to standard objects:

// pure ANSI-C++ hello world (II)

#include <iostream>

using namespace std;

 

int main () {

  cout << "Hello world in ANSI-C++\n";

  return 0;

}

Hello world in ANSI-C++

 

 

 

Back to CS 161 Homepage
This page was last modified January 22, 2001
wmorales@pcc.edu