C ++ namespace explanation

Source: Internet
Author: User

1. The namespace is used to solve the variable with the same name in different files.

For example, if you add using namespace STD;
The STD namespace does not need to add STD.
The header file iostream. H is designed to be compatible with C. C does not have the namespace concept at all, so you do not need to add using namespace STD;
In a popular way, there are many classes in a school. When I am looking for a student on the first day, what kind of class is I looking?
Using namespace: one-to-one class;
When looking for all the students in the top one class, you do not need to enter the top one class: No. 1.
You do not need to write
Using Class 1: 1;
Using: Class 2;

You can simply write it to No. 1.

Note that manyProgramThe user will not use using namespace STD; because many variables are defined in the standard namespace STD, they will be repeated with their own variables accidentally, so what can be used is added before:
Using STD: CIN;
Using STD: cout;
Using STD: Endl;
You can.

2. Cooperative Development

In fact, this is still very useful, especially in the development of team cooperation, for example, if you share the module with everyone, a writes a class W, B Also wrote a class W, but since there was no good communication before, their class names were all the same at last. When they were integrated, there was a conflict during the call. If the namespace is used, it is easy to do. During the call, you only need to say that the class W that calls a or the class W that calls B solves the problem.
For example, namespace A {class W {...};}
Namespace B {class W {...};}
When used:
A: w aw // generate the W object written by
Aw. Get ().....
B: w bw // generate the W object written by B
Bw. Get ()....
In this way, we will differentiate who is the class.

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.