Zweily's small piece (4) C ++ get started tutorial (3)

zhaozj2021-02-16  56

§3 Namespace STD

"Master, can you help me come over?"

I will know when I listen to this voice, my "good" apprentice YOUNG has used her very "magnetic" voice in summoning me, must have encountered any problems, it is impossible.

"Don't call my master, I feel like I am very old, or call me weily." I said, while walking, I saw a IDE, it seems to be written. Looking at it carefully, it turned out to debug the most basic, the most famous Hello World, Oh, the glasses on my nose are not used, I also see the problem.

"What is the problem?" I deliberately pretend what I don't know.

"Well ... I can't run this program, I don't know where it is wrong, I don't pass, I have an error, and I am in this program, I should play it in accordance with the book, it should be right! But it is ... "

"Oh? Let me see." I installed the way to see the display, the program on the display is like this:

#include int main () {cout << "hello world! / n"; return 0;}

Young also took a book to me, and it is indeed, the program on the book and the screen on the screen.

"You see, I didn't make a mistake?" Young looks innocently, it is rare to see her, this expression looks like a poor, and it is very different from usage.

"You don't worry, I know. First remind you, don't be superstitious, the book, when anyone has made mistakes, this is a good programmer should have the quality. This question, let me come a little bit Tell you to listen. You first look at the fault of the compiler report, remember, a good programmer can make full use of the compiler's error-in-one, and can judge the error message given from the compiler. The error is, so I have to learn to understand the error message, this is my most basic requirement for you. "

"Hey! It is the wrong mistake is 'cout': Undeclared Identifier. This time I have seen it when I learned C. It is when I use a variable that has not been declared in my program."

"It's completely correct! You have already said the wrong reason."

"Ah? But cout should be in the header file iostream already introduced this program, then there should be no statement?" Young was in a confused.

"In fact, this problem involves a feature inside C , is about the role of name space (Namespace)."

"Namespace? ..."

"Let us still look forward from your program. COUT is an Ostream object, which is an output stream object. It is indeed defined in iostream. However, its full name should be written into std :: cout. This STD is a namespace, Everything in all standard libraries is packaged in STD this Namespace. So, the compiler can't recognize COUT here because it is not in the global namespace. "

"Is that I just change Cout to std :: cout?"

"Well, this will be used after this change. However, this problem is like four ways of writing of the fennel beans of Kong Yi, and there are four solutions for this problem."

"Four kinds? What are the other three? Tell me!" It seems that this metaphor has caused younog interest.

"The first kind is to introduce STD namespace into the global name space. This method is as long as in the whole, that is, any function, of course, it is best to put it before the pre-processed, this, this, this There are things inside this in this function, just like using global objects. For example, you can change this. "# Include using namespace std; // introduced this Namespaceint Main ( ) {Cout << "Hello World! / N"; Return 0;}

"This program can be compiled, and the things in other standard libraries can be used directly as the cout here."

"Hey! So this method is not very convenient? It is not easy to make an error!"

"It's not the case. After doing this, STD named Namespace lost its existence. The purpose of introducing Namespace in C is to avoid pollution global name space, simply, to avoid and reduce naming conflicts. Once A program is big, it is difficult to avoid heavy names, especially multiplayer. The solution in the past C only rely relied manual attention, and extends the name to avoid replay. So will make some names There is no meaning or difficult to understand, and the programmer is also limited by this problem when writing the program, cannot be freely named the variables or functions of ourselves. There is no difference between NameSpace, this is C introduced Namespace. This concept is convenient. Of course, this also makes a lot of names in C looks particularly long. "

"Hey, it turns out! I am still ready to use it!"

"Let's discuss the second solution. This program is similar to the previous one, and it is also introduced in the name space. It is only to introduce a part of the need. Just like this program, we only need COUT, so we only need The introduction of COUT in the STD can be. Just like this. "

#include using std :: cout; int main () {cout << "Hello World! / N"; return 0;}

"Hey, it can be like this! This will avoid the problem of all the pollution names of the introduction. That's the last?"

"The last solution is simpler, however, I will pay attention to you first, this method has seen it today, don't have to remember it, let us use it. Let us take a look at the programs for this program. Sample. "

#include // Note that this header file is distinguished from the header files written in front of Int main () {cout << "Hello World! / N"; returnom 0;}

"The program of this method seems to be the most short. Yes, this header file is also the problem I always want to ask you. I remember the head files of those standard libraries in C. I ended. Why is C standard library of head files not. H? "

"You can notice this is very good, indicating that you are thinking with brain when reading books and writing procedures, and you can notice that many people tend to ignore the details. In fact, there is .H This suffix header file and not. The biggest difference in the header of H is also the Namespace, there is a .h header element is exposed to the global, that is, there is no STD this Namespace. The element in the header file is packaged. In the namespace in STD. In fact, the C Standard Commission has already removed each header file in the standard library. H suffix, just the manufacturer of the compiler, in order to make the program, in order to make the procedure before the standard can be used, Then still provides .h header files. Remember, these are not within C standards, this is why I don't want to use it. There is also the standards in the original C. The liberal file, in fact, there is also a corresponding C version, and their naming rules are simple, that is, with C, follow the original file name, but there is no .H This suffix. And, where the elements are also packaged In STD. "" Hey, there is still such a source! "

"Yes. There are many similar decisions and options in the development of C . Now those things defined in the standard are those who have been appreciated after thinking, and many of them have its truth, this you have If you are interested, you can take a look at "The Design and Evolution Of C " book, it can tell you why C is now like this. Actually, because C has its design idea and design philosophy, the standard library in the design It is in line with this idea, and it has maintained efficient and secure principles, so I use the standard library already provided in your program so that your code will be more short, easy to read, and more secure Remember this principle. "

"Well, I remember, and I will pay attention later."

"Okay, tell this today, there will be mentioned about other aspects of the standard library."

"Okay, thank you master!"

"How much is it to say, don't call me master, I haven't even my girlfriend, I am afraid that I am more old. Like I remember that I will tell you, remember me later. Weily, don't call Master! "

"Follow! Teacher ..."

"!"

"No, I didn't call you master, I just said 'is'" YOUNG is still so naughty.

"Okay, I will pay attention to it later. Continue to write your program, see your book."

"Good, weily. Thank you, thank you!"

"You don't have to be polite, who is SolmyR to give me it over ..."

After that, returned to your own seat, watching the cupped black tea on the table, hehe ...

转载请注明原文地址:https://www.9cbs.com/read-25059.html

New Post(0)