The role of C + + namespace

Source: Internet
Author: User

Namespace: namespace or name space, the traditional C + + has only a global namespace, but due to the current program size is getting larger and smaller, the global scope becomes more and more crowded, everyone may use the same name to implement different libraries, So programmers may have conflicting names when merging programs. namespace introduces complexity and solves this problem. namespace allows classes, objects, and functions to be clustered under a name, essentially namespace is a subdivision of the global scope.

For example, this procedure is often seen by:

1 #include <iostream>2usingnamespace  std; 3 4 int Main () 5 {6     printf ("Hello World! " ); 7     return 0 ; 8 }

Most probably know so much about namespace. But namespace far more than that, let me know more about namespace.

The basic format of namespace is:

namespace identifier
{
entities;
}

As an example,

Namespace Exp
{
int A, B;
}
It's a bit like a class, but it's completely two different types.
In order to use variables inside namespace outside of namespace we use:: operator, as follows
Exp::a
Exp::b

The use of namespace can effectively avoid redefining the problem.

As in the following example:

1#include <iostream>2 using namespacestd;3 4 namespace First5 {6   int var=5;7 }8 9 namespaceSecondTen { One   Double var=3.1416; A } -  - intMain () { thecout << First::var<<Endl; -cout << Second::var<<Endl; -   return 0; -}

The result of the output is:
5
3.1416
Two global variables are all names Var, but they are not in the same namespace so there is no conflict.

Keyword using can help introduce names from namespace to the current declaration area

1#include <iostream>2 using namespacestd;3 4 namespace First5 {6   intx =5;7   inty =Ten;8 }9 Ten namespaceSecond One { A   Doublex =3.1416; -   Doubley =2.7183; - } the  - intMain () { -   usingfirst::x; -   usingsecond::y; +cout << x <<Endl; -cout << y <<Endl; +cout << first::y <<Endl; Acout << second::x <<Endl; at   return 0; -}

Output is
5
2.7183
10
3.1416
As we specified the first X is First::x,y is SECOND.Y

The using can also import the entire namespace

1#include <iostream>2 using namespacestd;3 4 namespace First5 {6   intx =5;7   inty =Ten;8 }9 Ten namespaceSecond One { A   Doublex =3.1416; -   Doubley =2.7183; - } the  - intMain () { -   using namespaceFirst ; -cout << x <<Endl; +cout << y <<Endl; -cout << second::x <<Endl; +cout << second::y <<Endl; A   return 0; at}

Output is
5
10
3.1416
2.7183
As we foresee the namespace of the entire first, the previous pair of x, y values are the values of x, Y, and both.
Here we cannot add a "using namespace second" under the "using namespace first;"
This is tantamount to completely ignoring namespace first and namespace second, which results in a duplicate definition, so the preceding Hello_ The use directives in WORLD.C are somewhat problematic, just because we use a namspace, and once the new namespace is introduced, there is a good chance of repeating the definition.

In header files, we usually persist in using explicit qualification and confine the using directives to very small scopes, so that their utility is limited and easy to use. Similar examples are

1#include <iostream>2 using namespacestd;3 4 namespace First5 {6   intx =5;7 }8 9 namespaceSecondTen { One   Doublex =3.1416; A } -  - intMain () { the   { -     using namespaceFirst ; -cout << x <<Endl; -   } +   { -     using namespacesecond; +cout << x <<Endl; A   } at   return 0; -}

Output is
5
3.1416
You can see that two different namespace are restricted to different scopes, and there is no conflict between them.

namespace also supports nesting

1#include <iostream>2 3 namespace First4 {5     intA=Ten;6     intb= -;7 8     namespaceSecond9     {   Ten         DoubleA=1.02; One         Doubleb=5.002; A         voidhello (); -     }    -  the     voidSecond::hello () -     {    -Std::cout <<"Hello World"<<Std::endl; -     } + } -  + intMain () A { at     using namespaceFirst ; -  -std::cout<<second::a<<Std::endl; - Second::hello (); -}

Output is
1.02
Hello World
Nesting namespace Second,seond in namespace first is not straightforward and requires first to be used indirectly.

The role of C + + namespace

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.