A friend of the introductory series of C + + programming that has seen chicken peck rice should be able to notice that in many instances there is such a statement: using namespace STD, which uses the namespace STD, The function is to stipulate that the standard library functions used in the file are defined in the standard namespace Std. This article explains the meaning and usage of namespaces in detail.
the meaning of namespaces
Why do you need to define a function in a namespace? This is actually to avoid the problem of renaming variables or functions. Imagine that a project team of multiple engineers to develop, there may be global variables or function name of the phenomenon, and if everyone has defined their own namespace, you can solve the problem, even if the same name, as long as the separate namespace will not cause problems.
So it should be understood that namespaces are the inclusion of multiple variables and functions, so that they do not conflict with renaming any variables and functions outside the namespace.
Namespace Instance
Below the chicken peck Rice gives an instance of using a namespace. There are two namespaces in the instance, Jizhuomi and software, all of whom have a string named Szurl, and we try to access and output the two strings.
C + + code
- #include <iostream>
- Using namespace std;
- Namespace Jizhuomi
- Namespace Jizhuomi
- {
- char *szurl = "www.jizhuomi.com";
- }
- Namespace software
- Namespace software
- {
- char *szurl = "Www.jizhuomi.com/software/";
- }
- int _tmain (int argc, _tchar* argv[])
- {
- cout << jizhuomi::szurl << Endl; //Output a string in namespace Jizhuomi
- cout << software::szurl << Endl; //Output A string in namespace software
- return 0;
- }
This instance runs the result as:
We have seen that variables or functions in the namespace can be accessed using the namespace:: identifier name, and even repeated naming can be accessed correctly.
the meaning of using namespace *;
In the above example, when we access the string variables in the namespaces Jizhuomi and software, we need to precede the namespace::, but the cout is defined in the namespace STD, which is not preceded by "std::" Because the above statement "using namespace std; ".
The role of using namespace *; (which can be any namespace) is to release variables or functions in the namespace * so that it can be accessed without having to "namespace::", the access method is the same as the general variable or function, as in the above cout.
using namespace *; it will be convenient for us to write the program, but also use caution, if you release the contents of multiple namespaces, they may cause a naming conflict. Chicken Peck Rice gives you a demonstration of the simultaneous release of the Jizhuomi and software namespaces.
C + + code
- #include <iostream>
- Using namespace std;
- Namespace Jizhuomi
- Namespace Jizhuomi
- {
- char *szurl = "www.jizhuomi.com";
- }
- Namespace software
- Namespace software
- {
- char *szurl = "Www.jizhuomi.com/software/";
- }
- Release namespaces Jizhuomi and Software
- Using namespace Jizhuomi;
- Using namespace software;
- int _tmain (int argc, _tchar* argv[])
- {
- cout << szurl << Endl; //This will prompt for compilation errors
- return 0;
- }
In the example above, the compiler prompts for a compilation error because it does not know which namespace to access the string variable Szurl. In this case, you need to add a namespace modifier to the szurl before you want to access it correctly.
And what if a local variable szurl is defined in the main function?
C + + code
- #include <iostream>
- Using namespace std;
- Namespace Jizhuomi
- Namespace Jizhuomi
- {
- char *szurl = "www.jizhuomi.com";
- }
- Namespace software
- Namespace software
- {
- char *szurl = "Www.jizhuomi.com/software/";
- }
- Release namespaces Jizhuomi and Software
- Using namespace Jizhuomi;
- Using namespace software;
- int _tmain (int argc, _tchar* argv[])
- {
- char *szurl = "url";
- cout << szurl << Endl;
- return 0;
- }
After running we found that the Szurl access is correct, visible, in this case, the compiler first accesses the local variable.
A detailed description of the meaning and usage of namespaces in C + +