Namespaces and the ANSI/ISO C++
Standard Library
The namespace mechanism creates a collection of classes, free functions, constant declarations and so forth that
are qualified by a name. The syntax for a namespace declaration is:
namespace Name
{
declaration and definitions of classes, functions,
constants, etc.
}
In a program, the elements of a namespace are accessed by using the "::" operator. For instance, the namespace
MathConstants contains the definitions of two mathematical constants p and e. Access their value in the namespace
MathConstants using the syntax MathConstants::PI and MathConstants::E.
namespace MathConstants
{
const double PI = 3.141592653589793;
const double E = 2.718281828459045;
}
Namespaces permit the programmer to bundle a variety of C++ constructs in a named collection.
---------------------------------------------------
PROGRAM Namespaces-1 DECLARING AND USING NAMESPACES
---------------------------------------------------
This short program uses the namespace MathConstants and accesses the constants in the function main(). Assume
that the namespace declaration for MathConstants is stored in the header file "mconst.h".
#include
#include "mconst.h"
using namespace std;
int main()
{
cout << setreal(1,8) << MathConstants::PI << endl
<< setreal(1,10) << MathConstants::E << endl;
return 0;
}
Run:
3.14159265
2.7182818285
Duplicate Names
Two namespaces may contain elements with the same name. This can occur when you install two different software
packages that support classes, functions, or constant declarations that use the same names. For instance, consider
the two namespaces MathConstants and ShortConstants. The two namespaces provide constants of the same name
but with different precision.
namespace MathConstants
{
const double PI = 3.141592653589793;
const double E = 2.718281828459045;
}
namespace ShortConstants
{
const double PI = 3.14159;
const double E = 2.71828;
const double ROOT2 = 1.41421;
}
A program references PI in MathConstants using the expression MathConstants::PI and PI in ShortConstants
using the expression ShortConstants::PI.
The Keyword using
The keyword using allows the elements of a single namespace to be referenced without the "::" operator. All
elements of any other namespace must use the "::" operator. The syntax for the keyword is
using namespace Name;
If the code provides the statement
using namespace MathConstants;
a program statement
cout << E << " " << ShortConstants::E
<< ShortConstants::ROOT2 << endl;
outputs the value of E from MathConstants. The references to a constants E and ROOT2 in the namespace
ShortConstants must use the "::" operator.
The C++ Standard Library
ANSI (American National Standards Institute) and ISO (International Standards Organization) cooperate to develop a
worldwide standard for the C++ language called the ANSI/ISO C++ Standard. This standard defines a C++ library
that includes the Standard Template Library (STL) of container classes and the string class. The library bundles the
components in a namespace called std. The C++ Standard library provides 32 C++ header files, as shown in Table
Namespaces1.
Table Namespaces1 C++ Library Header Files
阅读(1198) | 评论(0) | 转发(0) |