A detailed description of the meaning and usage of namespaces in C + +

Source: Internet
Author: User

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
  1. #include <iostream>
  2. Using namespace std;
  3. Namespace Jizhuomi
  4. Namespace Jizhuomi
  5. {
  6. char *szurl = "www.jizhuomi.com";
  7. }
  8. Namespace software
  9. Namespace software
  10. {
  11. char *szurl = "Www.jizhuomi.com/software/";
  12. }
  13. int _tmain (int argc, _tchar* argv[])
  14. {
  15. cout << jizhuomi::szurl << Endl; //Output a string in namespace Jizhuomi
  16. cout << software::szurl << Endl; //Output A string in namespace software
  17. return 0;
  18. }

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
  1. #include <iostream>
  2. Using namespace std;
  3. Namespace Jizhuomi
  4. Namespace Jizhuomi
  5. {
  6. char *szurl = "www.jizhuomi.com";
  7. }
  8. Namespace software
  9. Namespace software
  10. {
  11. char *szurl = "Www.jizhuomi.com/software/";
  12. }
  13. Release namespaces Jizhuomi and Software
  14. Using namespace Jizhuomi;
  15. Using namespace software;
  16. int _tmain (int argc, _tchar* argv[])
  17. {
  18. cout << szurl << Endl; //This will prompt for compilation errors
  19. return 0;
  20. }

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
  1. #include <iostream>
  2. Using namespace std;
  3. Namespace Jizhuomi
  4. Namespace Jizhuomi
  5. {
  6. char *szurl = "www.jizhuomi.com";
  7. }
  8. Namespace software
  9. Namespace software
  10. {
  11. char *szurl = "Www.jizhuomi.com/software/";
  12. }
  13. Release namespaces Jizhuomi and Software
  14. Using namespace Jizhuomi;
  15. Using namespace software;
  16. int _tmain (int argc, _tchar* argv[])
  17. {
  18. char *szurl = "url";
  19. cout << szurl << Endl;
  20. return 0;
  21. }

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 + +

Related Article

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.