The use of namespace

Source: Internet
Author: User

A single global variable namespace is used in C + +. In this single space, if there are two variables or functions whose names are exactly the same, there will be a conflict. Of course, you can also use different names, but sometimes we don't know that the other variable uses exactly the same name; sometimes it is necessary to use the same name for the convenience of the program. For example, if you define a variable string user_name, it is possible to define a variable of the same name in a library file or in another program code that you call, and this will cause a conflict. Namespaces are services that address the naming conflicts of variables and functions in C + +. The solution is to define your strtemp variable in a different name namespace. It's like a TV set at home, and Lee has the same type of TV, but we can distinguish it because they belong to different families.
For example:

#include"stdafx.h"#include<iostream>#include<string>using namespacestd;//Using namespace compilation instructions to make the names defined in the C + + standard class library available in this program//Otherwise, the C + + standard classes such as iostream,string are not visible, and compilation will go wrong. //two variables with the same name defined in different namespacesnamespacemyown_y{stringUser_name ="Myown_yuan";}namespacemyown_z{stringUser_name ="Myown_zhang";}intMain () {cout<<"\ n"<<"Hello,"<< Myown_y::user_name//access the variable with the namespace limiter myown_y user_name<<"... and goodbye!\n"; cout<<"\ n"<<"Hello,"<< Myown_z::user_name//access the variable with the namespace limiter myown_z user_name<<"... and goodbye!\n"; return 0;}

Of course, we can also use the pre-compilation instructions at the beginning of the program to work with the names in the namespace. The advantage of using precompiled instructions is that you do not have to explicitly use namespace restrictions to access variables in your program. The above main program can be modified to:

#include"stdafx.h"#include<iostream>#include<string>using namespacestd;//Using namespace compilation instructions to make the names defined in the C + + standard class library available in this program//Otherwise, the C + + standard classes such as iostream,string are not visible, and compilation will go wrong. //two variables with the same name defined in different namespacesnamespacemyown_y{stringUser_name ="Myown_yuan";}namespacemyown_z{stringUser_name ="Myown_zhang";}intMain () {using namespacemyown_y; cout<<"\ n"<<"Hello,"<<user_name<<"... and goodbye!\n"; //using namespace myown_z;cout<<"\ n"<<"Hello,"<< Myown_z::user_name//access the variable with the namespace limiter myown_z user_name<<"... and goodbye!\n"; return 0;}

But the second variable must be accessed with a namespace restriction, because the variables in the myown_y space are already visible, and if unrestricted, the compiler will not recognize the variable in that namespace. This point must be noted. These are just a few of the concepts that beginners are not aware of and will continue to discuss other concepts in future articles.

The following is a complete program to show the use of this function;

#include <iostream>using namespacestd;namespacecui{voidLove () {cout<<"He love a wonderful girl, and she name is Shili"<<Endl;}}using namespace Cui;intMain () {intA=Ten; cout<<a<<Endl;    Love (); Cui::love ();//Call Mode 2    return 0;}

The use of 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.