A program may include many identifiers defined in different scopes. Sometimes a variable of one scope will “overlap” (i.e., collide) with a variable of the same name in a different scope, possibly creating a naming conflict. Such overlapping can occur at many levels. Identifier overlapping occurs frequently in third-party libraries that happen to use the same names for global identifiers (such as functions). This can cause compilation errors.
C++ solves this problem with namespaces. Each namespace
defines a scope in which identifiers and variables are placed. To use a namespace member, either the member’s name must be qualified with the namespace
name and the scope resolution operator (::), as in
MyNameSpace::member
or a using
directive must appear before the name is used in the program. Typically, such using
statements are placed at the beginning of the file in which members of the namespace
are used. For example, placing the following using
directive at the beginning of a source-code file
using namespace MyNameSpace;
specifies that members of namespace
MyNameSpace can be used in the file without preceding each member with MyNameSpace and the scope resolution operator (::
).
A using
directive of the form
using std::cout;
brings one name into the scope where the directive appears. A using
directive of the form
using namespace std;
brings all the names from the specified namespace
(std
) into the scope where the directive appears.
Error-Prevention Tip 21.2
Precede a member with its namespace name and the scope resolution operator (::
) if the possibility exists of a naming conflict.
Not all namespaces are guaranteed to be unique. Two third-party vendors might inadvertently use the same identifiers for their namespace
names. Figure 21.3 demonstrates the use of namespace
s.
1 // Fig. 21.3: fig21_03.cpp
2 // Demonstrating namespaces.
3 #include <iostream>
4 using namespace std;
5
6 int integer1 = 98; // global variable
7
8 // create namespace Example
9 namespace Example
10 {
11 // declare two constants and one variable
12 const double PI = 3.14159;
13 const double E = 2.71828;
14 int integer1 = 8;
15
16 void printValues(); // prototype
17
18 // nested namespace
19 namespace Inner
20 {
21 // define enumeration
22 enum Years { FISCAL1 = 1990, FISCAL2, FISCAL3 };
23 } // end Inner namespace
24 } // end Example namespace
25
26 // create unnamed namespace
27 namespace
28 {
29 double doubleInUnnamed = 88.22; // declare variable
30 } // end unnamed namespace
31
32 int main()
33 {
34 // output value doubleInUnnamed of unnamed namespace
35 cout << "doubleInUnnamed = " << doubleInUnnamed;
36
37 // output global variable
38 cout << "
(global) integer1 = " << integer1;
39
40 // output values of Example namespace
41 cout << "
PI = " << Example::PI
<< "
E = " << Example::E
42 << "
integer1 = " << Example::integer1 << "
FISCAL3 = "
43 << Example::Inner::FISCAL3
<< endl;
44
45 Example::printValues(); // invoke printValues function
46 } // end main
47
48 // display variable and constant values
49 void Example::printValues()
50 {
51 cout << "
In printValues:
integer1 = " << integer1 << "
PI = "
52 << PI << "
E = " << E << "
doubleInUnnamed = "
53 << doubleInUnnamed << "
(global) integer1 = " << ::integer1
54 << "
FISCAL3 = " << Inner::FISCAL3 << endl;
55 } // end printValues
3.145.179.85