0% found this document useful (0 votes)
4 views

Namespaces

C++

Uploaded by

amjad
Copyright
© © All Rights Reserved
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)
4 views

Namespaces

C++

Uploaded by

amjad
Copyright
© © All Rights Reserved
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
You are on page 1/ 14

Namespaces

• When writing a class library, programmers would prefer to use short


and common names for non-member functions and classes, like
add() and book().
• However, short and common names may turn out to be the same
names selected by the creators of another library or by an
application that uses the library.
• This can lead to “name clashes” and generate multiple definition
errors from your compiler.
• Before the advent of namespaces, programmers were forced to use
long names to avoid this problem:
Henry’s_Simplified_Statistics_Library_add();
• However, long names are difficult to read and write and take up
excessive space in a listing.
• Namespaces can solve this problem. (Note that member functions
don’t cause name clashes because their scope is limited to the
class.)
Defining a Namespace
• A namespace is a section of a file that is given a
name. The following code defines a namespace
geo with some declarations inside it:
namespace geo
{
const double PI = 3.14159;
double circumf(double radius)
{ return 2 * PI * radius; }
} //end namespace geo
• Braces delimit the namespace.
• Variables and other program elements
declared within the braces are called
namespace members.
• Notice that there is no semicolon following
the closing brace, as there is with classes.
Accessing Namespace Members
• Code outside a namespace cannot access the
elements within it, at least not in the normal
way.
• The namespace makes them invisible:
namespace geo
{
const double PI = 3.14159;
double circumf(double radius)
{ return 2 * PI * radius; }
} //end namespace geo
double c = circumf(10); //won’t work here
• To make the elements visible outside the
namespace you must invoke the namespace
name when referring to them.
• There are two ways to do this.
• First, you can precede each element’s name with
the namespace name and the scope resolution
operator:
double c = geo::circumf(10); //OK
Or you can use the using directive:
using namespace geo;
double c = circumf(10); //OK
• The using directive ordinarily causes the namespace to be visible from
that point onward.
• However, you can restrict the region where the using directive is in effect
to a particular block, such as a function:

void seriousCalcs()
{
using namespace geo;
//other code here
double c = circumf(r); //OK
}
double c = circumf(r); //not OK
Here the members of the namespace are visible only within the function
body.
Namespaces in Header Files
• Namespaces are most commonly used in
header files containing library classes or
functions.
• Each such library can have its own namespace.
• By this time you are familiar with the
namespace std, whose members constitute
the Standard C++ Library.
Multiple Namespace Definitions
• There can be several instances of the same namespace definition:
namespace geo
{
const double PI = 3.14159;
} // end namespace geo
//(some other code here)

namespace geo
{
double circumf(double radius)
{ return 2 * PI * radius; }
} //end namespace geo
• This looks like a redefinition, but it’s really just a continuation of the
same definition.
• It allows a namespace to be used in several header files, which can
then all be included in a source file.
Example
In the Standard C++ Library, dozens of header files use the namespace std.
//fileA.h
namespace alpha
{
void funcA();
}
//fileB.h
namespace alpha
{
void funcB();
}
fileMain.cpp
#include “fileA.h”
#include “fileB.h”
using namespace alpha;
funcA();
funcB();
Declarations Outside a namespace
• You can place declarations outside a namespace
that behave as if they were inside it.
• All you need is the scope resolution operator and
the namespace name:
namespace beta
{
int uno;
}
int beta::dos;
• Here, both uno and dos are declared in the
namespace beta.
Unnamed Namespaces
• You can create a namespace without a name.
• Doing so creates a namespace that is
automatically visible throughout the file in which
it’s defined, but not visible from other files.
• The compiler gives an unnamed namespace an
internal name unique to the file.
• Elements declared in the unnamed namespace
can be accessed from anywhere in the file.
• In the following listing, funcA() and funcB() can
access the gloVar variable in their respective files.
Example
//fileA.cpp
namespace //unnamed namespace unique to fileA.cpp
{
int gloVar = 111;
}
funcA()
{ cout << gloVar; } //displays 111
//fileB.cpp
namespace //unnamed namespace unique to fileB.cpp
{
int gloVar = 222;
}
funcB()
{ cout << gloVar; } //displays 222
• In this example both files contain a variable named gloVar, but there’s no conflict
because each variables is declared in an unnamed namespace unique to its file
and is invisible everywhere else.
• This approach provides an alternative to the use of static for restricting the scope
of global variables to their own file. In fact, the namespace approach is now
considered preferable to making elements static.

You might also like