0% found this document useful (0 votes)
63 views1 page

Namespaces

Namespaces can be used to avoid naming conflicts between independently developed libraries. When multiple libraries define the same global variable name, like "listCount", it causes compiler errors. This problem can be solved by defining each library's variables within their own namespace, such as "LibraryA" and "LibraryB". Then a using declaration brings a specific namespace and its variables into the current scope without conflicts with other namespaces.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views1 page

Namespaces

Namespaces can be used to avoid naming conflicts between independently developed libraries. When multiple libraries define the same global variable name, like "listCount", it causes compiler errors. This problem can be solved by defining each library's variables within their own namespace, such as "LibraryA" and "LibraryB". Then a using declaration brings a specific namespace and its variables into the current scope without conflicts with other namespaces.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link]

Identify the Basic Differences Between C and C++


Uses of Multiple Namespaces

A programmer who uses several independently developed libraries may encounter naming clashes by accident. Suppose that library A
defines a global variable called listCount and library B also defines a global variable called listCount. If the developer attempts
to use both libraries A and B at the same time, it can lead to problems. The compiler will complain that listCount is defined more than
once at the application level. This problem can be solved by using multiple namespaces.

Additional namespaces may be created using the namespace keyword. The use of multiple namespaces for the conflicting libraries
allows a developer to easily segregate variable names and thus avoid conflicts.

An example is:
namespace LibraryA {
int listCount;
}

namespace LibraryB {
int listCount;
}
The example above shows two snippets of code, defining separate namespaces LibraryA, and LibraryB and a variable
listCount within each.

A subsequent using declaration brings the variable in a given namespace into the current scope, or visibility, of your application
(without fear of conflict), as shown below:
using LibraryA::listCount;
ListCount = 25;

1 of 1 3/11/2011 10:15 AM

You might also like