Namespace Chinese meaning is a namespace or name space, the traditional C + + has only a global namespace, but because of the scale of the current program, the division of the program more and more thin, the global scope becomes more and more crowded, everyone may use the same name to implement different libraries, So the programmer may have conflicting names when merging the program. namespace introduces complexity and solves this problem. namespace allows classes, objects, and functions to be clustered under a name. Essentially speaking, namespace is a subdivision of the global scope.
I think everyone has seen such a procedure:
Hello_world.c
#include <iostream>
using namespace Std;
int main ()
{
printf ("Hello World!");
return 0;
}
I think a lot of people know namespace so much.
But namespace far more than that, let's learn more about namespace
The basic format of the namespace format is
namespace identifier
{
entities;
}
To give 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
#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
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
#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;
}
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
#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;
}
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
#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;
}
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
#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 ();
}
Output is
1.02
Hello World
Nesting namespace Second,seond in namespace first is not straightforward and requires first to be used indirectly.
Namespace can use aliases, it is a pleasant thing to use aliases for some long-named namespace. However, as with using, it is best to avoid using namespace aliases in header files (f is more prone to conflict than first).
namespace F = First;
Finally, namespace provides a separate scope, similar to the use of static global declarations, which can be implemented using unnamed namespace definitions:
namespace {int count = 0;} The count here is unique.
Count is valid in other parts of the program
void chg_cnt (int i) {count = i;}
Namespace for C + +