Namespace in C ++

Source: Internet
Author: User
Namespace is a namespace or namespace. The traditional C ++ only has one global namespace, global scopes become increasingly crowded, and everyone may use the same name to implement different libraries. Therefore, programmers may encounter name conflicts when merging programs. Namespace introduces complexity and solves this problem. Namespace allows classes, objects, and functions to aggregate under a single name. Essentially, namespace is a subdivision of the global scope.

I think everyone has seen such a program:
Hello_world.c
# Include <iostream>
Using namespace STD;

Int main ()
{
Printf ("Hello world! ");
Return 0;
}
I think many people know so much about namespace.
But namespace is far more than that. Let's take a look at namespace.

The basic format of namespace is
Namespace identifier
{
Entities;
}
For example,
Namespace exp
{
Int A, B;
}
It is a bit similar to a class, but it is completely two different types.
To use the variables in the namespace outside the namespace, we use the: operator, as shown below:
Exp:
Exp: B
Using namespace can effectively avoid redefinition Problems
# Include <iostream>
Using namespace STD;

Namespace first
{
Int Var = 5;
}

Namespace second
{
Double Var = 3.1416;
}

Int main (){
Cout <first: var <Endl;
Cout <Second: var <Endl;
Return 0;
}
The result is
5
3.1416
Both global variables are named var, but they are not in the same namespace, so there is no conflict.

The keyword using can help introduce the name from the namespace to the current declared area.
# Include <iostream>
Using namespace STD;

Namespace first
{
Int x = 5;
Int y = 10;
}

Namespace second
{
Double X = 3.1416;
Double Y = 2.7183;
}

Int main (){
Using First: X;
Using Second: Y;
Cout <x <Endl;
Cout <Y <Endl;
Cout <first: y <Endl;
Cout <Second: x <Endl;
Return 0;
}
The output is
5
2.7183
10
3.1416
For example, the first X we specify is first: X, and Y is second. Y.

Using can also import the entire namespace.
# Include <iostream>
Using namespace STD;

Namespace first
{
Int x = 5;
Int y = 10;
}

Namespace second
{
Double X = 3.1416;
Double Y = 2.7183;
}

Int main (){
Using namespace first;
Cout <x <Endl;
Cout <Y <Endl;
Cout <Second: x <Endl;
Cout <Second: y <Endl;
Return 0;
}
The output is
5
10
3.1416
2.7183
As we foresee, the first namespace of the entire import, the first pair of X, the value of Y is the value of x and y in first.
Here we cannot add "using namespace second;" in "using namespace first;". Why?
This is tantamount to directly ignoring namespace first and namespace second, and repeated definitions will appear. Therefore, the using command in the preceding hello_world.c has problems to some extent, the reason is that we use a namspace. Once a new namespace is introduced, the problem of repeated definition may occur.

In header files, we usually stick to explicit limits and only limit the using commands to a very small scope, so that their utility will be limited and easy to use. Similar examples include:
# Include <iostream>
Using namespace STD;

Namespace first
{
Int x = 5;
}

Namespace second
{
Double X = 3.1416;
}

Int main (){
{
Using namespace first;
Cout <x <Endl;
}
{
Using namespace second;
Cout <x <Endl;
}
Return 0;
}
The output is
5
3.1416
We can see that two different namespaces are restricted to different scopes, so there is no conflict between them.

Namespace also supports nesting
# Include <iostream>

Namespace first
{
Int A = 10;
Int B = 20;

Namespace second
{
Double A = 1.02;
Double B = 5.002;
Void Hello ();
}

Void Second: Hello ()
{
STD: cout <"Hello World" <STD: Endl;
}
}

Int main ()
{
Using namespace first;

STD: cout <Second: A <STD: Endl;
Second: Hello ();
}
The output is
1.02
Hello World
Namespace second is nested in namespace first, and seond cannot be used directly. First is required for indirect use.

Namespace can use aliases. It is a pleasure to use aliases for Long-name namespaces. But like using, it is better to avoid using the namespace alias in the header file (F is more prone to conflicts than first ).
Namespace F = first;

Finally, namespace provides a separate scope, which is similar to the use of static global declarations. It can be implemented using an unnamed namespace definition:
Namespace {int COUNT = 0;} // The count here is unique
// In other parts of the program, count is valid.
Void chg_cnt (int I) {COUNT = I ;}

References:
C ++: Yes, yes. [us] Stephen C. Dewhurst, honor translation clause 23 namespace
Http://www.cplusplus.com/doc/tutorial/namespaces.html
C ++ essence [beauty] Ira Pohl translated by Wang shuwu, Chen Shuying, and others p15

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.