C + + namespaces
In a C + + application. For example, you might write a function called Func () that also has the same function func () in another available library. This way, the compiler will not be able to tell which func () function you are using.
Therefore, the concept of namespaces is introduced to solve the above problem, which can be used as additional information to distinguish functions, classes, variables, etc. of the same name in different libraries. The context is defined by using a namespace. In essence, a namespace is defined as a scope.
Define namespaces
The definition of a namespace uses the keyword namespace, followed by the name of the namespace, as follows:
To invoke a function or variable with a namespace, precede it with the name of the namespace, as follows:
Name::code; //code can be a variable or function and name is the name of the namespace
eg
#define_crt_secure_no_warnings#include<iostream>namespacespace_test1{typedefintT; intA = +; voidFucvoid) {Std::cout<<"I am space_test1 \ n"; }}namespacespace_test2{typedefDoubleT; intA = -; voidFucvoid) {Std::cout<<"I am space_test2\n"; }}intMain () {//The same function, variable, type, can be used with the different namespacesSpace_test2::t High =1.1; Space_test1::t Age= -; Space_test2::fuc (); Space_test1::fuc (); Std::cout<<"high="<< High <<Std::endl; Std::cout<<"age="<< age<<Std::endl; Std::cout<<"space_test1::a="<< space_test1::a <<Std::endl; Std::cout<<"space_test2::a="<< space_test2::a <<Std::endl; System ("Pause"); return 0;}
using Directives
You can use the Using namespace directive so that you can use a namespace without having to precede the name of the namespace. This instruction tells the compiler that subsequent code will use the name in the specified namespace.
Why we do not recommend using namespace ... The practice of:
Namespaces use such direct declarations, destroying the same name functions, variables, and classes that it wants to solve, although it is easier to do this without causing ambiguity, since it is possible to enter fewer characters, but it is more appropriate to use a namespace prefix if the same function, variable, class is not guaranteed.
C + + Namespace---namespace