Note: Section 2.7
For most people, the first step in learning and programming is to imitate, and the direct point is to copy a few pieces of code from the textbook (such as the well-known Hello World to the machine, then slow habits, good, this is A good way to learn languages. But there is a problem here, people have never thinking about things that have become habit, resulting in a long time, I don't know about its specific meaning for themselves almost every day, and give an example: as long as you use It is C , then I don't know how to write such a sentence every time: "Using Namespace Std;", many beginners who see the C source program will ask: What is it? Those who truly study for a while have not felt this problem, but the truly thoughts should be the latter, this can't be said to be a big drawback of this learning method, this is also my first to figure out the basics The reason for the concept.
As I said in the note: In order to cooperate, it makes the programmaken one of the most unique behaviors in this world, so the name of the programmer is often almost the same time when the programmer is in the naming component. The global name is inevitable, then the probability of name pollution is very large, understanding these, I believe that the understanding of the concept of Namespac should be not a problem, nothing more than the visibility of the name, of course, this is not our most concerned , We want to know how to use it. This will solve the following two problems.
First: How to define? A more clear answer is given in the book:
Namespace owl {
INT X;
Char y;
Class Obj {... ..};
Void max (const Int *);
}
But such an answer, we are not very satisfied, for example, we all know that in order to reduce the compilation time and avoid repetition function definitions. Often the declaration of the function in the header file (abc.h), and put the implementation in the corresponding source file (ABC.cpp). So how do name spaces define? Is there a "{" in abc.h, a "}" in ABC, .cpp? If you can really do, how do you do more than one header? Obviously this is impossible, this we can explain in 8.5. Now we have such a problem, and leave it in your mind, don't let this concept become numb habits.
Second: How to use it? This section gives three usage. Here we make a simple comparison through the Hello World program.
1. USING prompt:
#include
#include
Using namespace std;
int main ()
{
COUT << "Hello World!" << endl;
System ("pause");
Return 0;
}
Ha ha. Very comfortable? right? Yes it is. Most people are this usage, but as mentioned in the book, this practice makes the name space shaped with virtual, I have encountered such a thing. Once I wrote Main (), I forgot to write the USING prompt, and the compiler actually passed me. I was really scared. Later I found out that I include INCLUDE file has been written. It can be seen that this way of writing, in case I don't want to use this name space, INCLUDE is a bit trouble. Of course, for STD, this is a little excessive, because no one will go to the ISO to grab the name. (Well, except for some special people) 2. Namespace modifier
#include
#include
int main ()
{
Std :: cout << "Hello World!" << std :: end1
System ("pause");
Return 0;
}
This method is good. It's too annoying, every time I have to write STD, the longer the process, the longer the boss, the boss does not add salary, and I don't do it. (What? Name? Namespac a = std;? Do you do it. 咱 still not dry)
3. USING statement.
#include
#include
Using std :: cout;
Using std :: end1;
int main ()
{
COUT << "Hello World!" << endl;
System ("pause");
Return 0;
}
Yep. Only declare it, this is not bad. Although more written, the longer the program, the more you write, huh, huh. Wait, such words. If the declaration is in the header file, don't you go back to the first question, you can change it again.
#include
#include
int main ()
{
Using std :: cout;
Using std :: end1;
COUT << "Hello World!" << endl;
System ("pause");
Return 0;
}
Ah, wonderful, using can further change the name of the name, this time you will definitely ask, can the USING prompt can be used like this? If you can, do you not save? Unfortunately, you can't, you will find the answer in 8.5.