67 048
Assignments Done
99,3%
Successfully Done
In November 2018

Answer to Question #7899 in C++ for Jie

Question #7899
The using namespace statement is commonly used in programs to simplify the code involving C++ header files.Is really necessary to code your own namespaces in programs. Can you tell me some advantages and disadvantages of user-defined namespaces.
Expert's answer
namespace

The namespace keyword allows you to create a new scope. The
name is optional, and can be omitted to create an unnamed namespace. Once you
create a namespace, you'll have to refer to it explicitly or use the using
keyword. A namespace is defined with a namespace block.
Syntax
namespace
name {
declaration-list;
}

In many programming languages, a
namespace is a context for identifiers. C++ can handle multiple namespaces
within the language. By using namespace (or the using namespace keyword), one is
offered a clean way to aggregate code under a shared label, so as to prevent
naming collisions or just to ease recall and use of very specific scopes. There
are other "name spaces" besides "namespaces"; this can be confusing.

Name
spaces (note the space there), as we will see, go beyond the concept of scope by
providing an easy way to differentiate what is being called/used. As we will
see, classes are also name spaces, but they are not namespaces.
Note:
Use
namespace only for convenience or real need, like aggregation of related code,
do not use it in a way to make code overcomplicated for you and
others

Example
namespace foo {
int bar;
}

Within this
block, identifiers can be used exactly as they are declared. Outside of this
block, the namespace specifier must be prefixed (that is, it must be qualified).
For example, outside of namespace foo, bar must be written foo::bar.

C++
includes another construct which makes this verbosity unnecessary. By adding the
line using namespace foo; to a piece of code, the prefix foo:: is no longer
needed.
unnamed namespace

A namespace without a name is called an
unnamed namespace. For such a namespace, a unique name will be generated for
each translation unit. It is not possible to apply the using keyword to unnamed
namespaces, so an unnamed namespace works as if the using keyword has been
applied to it.
Syntax
namespace {
declaration-list;
}
namespace
alias

You can create new names (aliases) for namespaces, including nested
namespaces.
Syntax
namespace identifier =
namespace-specifier;


using namespaces
using
using namespace
std;

This using-directive indicates that any names used but not declared
within the program should be sought in the ‘standard (std)'
namespace.
Note:
It is always a bad idea to use a using directive in a
header file, as it affects every use of that header file and would make
difficult its use in other derived projects; there is no way to "undo" or
restrict the use of that directive. Also don't use it before an #include
directive.


To make a single name from a namespace available, the
following using-declaration exists:
using foo::bar;

After this
declaration, the name bar can be used inside the current namespace instead of
the more verbose version foo::bar. Note that programmers often use the terms
declaration and directive interchangeably, despite their technically different
meanings.

It is good practice to use the narrow second form (using
declaration), because the broad first form (using directive) might make more
names available than desired. Example:
namespace foo {
int bar;
double
pi;
}

using namespace foo;

int* pi;
pi = &bar; //
ambiguity: pi or foo::pi?

In that case the declaration using foo::bar;
would have made only foo::bar available, avoiding the clash of pi and foo::pi.
This problem (the collision of identically-named variables or functions) is
called "namespace pollution" and as a rule should be avoided wherever
possible.

using-declarations can appear in a lot of different places.
Among them are:
namespaces (including the default
namespace)
functions

A using-declaration makes the name (or namespace)
available in the scope of the declaration. Example:
namespace foo
{
namespace bar {
double pi;
}

using bar::pi;
// bar::pi can
be abbreviated as pi
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
24.01.13, 14:23

Dear visitor
If something is unclear to you, don't hesitate and ask questions

Jane Maerylle V Laroza
24.01.13, 04:50

sohard........ :O

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions