using, namespace, and std

The next line, using namespace std;, will be present in nearly all of our programs. It tells the compiler to treat the names in the standard library, a very important part of the C++ language definition, as though we had defined them in the current program. These names from the standard library include string, which is why we need using namespace std; here. I'll go into this in much more detail later, but in the meantime, here's a little information about using, namespace, and std.

A namespace is a collection of identifiers (variable names and some other types of names that we haven't discussed yet) that all belong to a “family” of sorts. To refer to a specific identifier that belongs to a namespace, you can prefix the identifier with the name of the namespace followed by two colons.

For example, all of the identifiers in the C++ standard library are in the namespace called std. So, for example, if we want to refer to the version of string that is in the namespace called std, we can refer to it by the name std::string.

However, because the standard library identifiers are used so frequently, it is annoying to have to say std:: every time we want to refer to one of them. The designers of the language have anticipated this problem and have provided the using facility to allow us to write our programs in a more natural way. What using means is that we want the compiler to add a certain name or set of names from a particular namespace to the currently accessible set of names. In this particular case, using namespace std; means that we want the compiler to make all the names in the standard library accessible. If we left that line out, whenever we wanted to create a variable of string type, we would have to write std::string rather than just string, and the same would be true of the other types defined in the standard library that we will be using later. I think we have enough details to keep track of without having to worry about that particular one, so I have included the line using namespace std; in all of the example programs except where I have a specific reason not to do so; I'll note those cases when we get to them.

As you can probably imagine, Susan wasn't entirely satisfied with this explanation, even for the time being. Here's the discussion we had about it:

Susan: I am not sure I understand this. In other words you just say using namespace std; and then every variable you use is going to be from the standard library?

Steve: Not quite. When we say using namespace std;, that means "add the names from the standard library to the names we have available for use". For example, after that statement, if I say cout, I mean std::cout, and if I say string, I mean std::string. Names not defined in the standard library aren't affected by this.

Susan: Isn't that what I said? My question is, if you don't want to use the standard library, how do you indicate that?

Steve: No, what you said is that "every" variable is from the standard library. It's only things that have names defined in the standard library that are affected. However, if you have something called "string", for example, that is defined in the standard library and also in your code, you can say "::string" if you don't want to use the one from the standard library. We'll see an example of this in “The Scope Resolution Operator” on page 269 in Chapter 5.

Susan: Okay.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset