Namespaces

In C++, it is possible to create a local scope using the namespace keyword. A namespace defines a declarative region. Its purpose is to localize names. The general form of namespace is shown here:

namespace name {
 // ...
}

Here, name is the name of the namespace. For example,

namespace MyNameSpace {   int count; }

This creates a namespace called MyNameSpace and the variable count is declared inside it.

Names declared within a namespace can be referred to directly by other statements within the same namespace. Outside their namespace, names can be accessed two ways. First, you can use the scope resolution operator. For example, assuming MyNameSpace just shown, the following statement is valid:

MyNameSpace::count = 10;

You can also specify a using statement, which brings the specified name or namespace into the current scope. For example,

using namespace MyNameSpace; count = 100;

In this case, count can be referred to directly because it has been brought into the current scope.

When C++ was originally invented, items declared in the C++ library were in the global (i.e., unnamed) namespace. However, Standard C++ puts all of these items into the std namespace.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net