Namespaces (C++的名字空间)

来源:互联网 发布:众途软件骗局 编辑:程序博客网 时间:2024/04/29 19:59

Namespaces (名字空间)
As mentioned in Chapter 1, one of the problems encountered in the
在第一章中我们曾提到,在C语言中常碰到的一个问题就是当自己写的程序大到
C language is that you “run out of names” for functions and
一定程度时,对函数与一些标识符的命名,我们用完名字(Q1.这是什么?什么叫用完?)
identifiers when your programs reach a certain size. Of course, you
当然也不是你真正地用完了名字;情况只是得过一段时间才能想出新的来。
don't really run out of names; it does, however, become harder to
think of new ones after awhile. More importantly, when a program
更为重要的是,一个程序大到一定程度后,
reaches a certain size it's typically broken up into pieces, each of
它通常会被分成好多子项目,每一个子项目
which is built and maintained by a different person or group. Since
都分别由不同的人或小级来完成与维护。
C effectively has a single arena where all the identifier and function
由于C语言只有一个工作台来定义所有的标识符与函数
names live, this means that all the developers must be careful not to
这样也就意味着所有的程序员必须小心处理,以避免那些相同的名字造成的冲突。
accidentally use the same names in situations where they can conflict.
This rapidly becomes tedious, time-wasting, and,ultimately, expensive.
对这种情况的解决后来变得越来越麻烦,费时,当然最终也很不花算。

Standard C++ has a mechanism to prevent this collision: the namespace
现在标准的C++引进了一新的机制,即“名字空间”关键字,来避免这种的冲突
 keyword. Each set of C++ definitions in a library or program is “wrapped”
每一个C++库函数与程序的定义都包装在一个名字空间中,
 in a namespace, and if some other definition
这样,要是有相同的定义但不在同一个名字空间时,就不会发生名字的冲突。
has an identical name, but is in a different namespace, then there is
no collision.
Namespaces are a convenient and helpful tool, but their presence
名字空间应用起来很方便也很有效,但在写程序之前,我们得对它们的存在
means that you must be aware of them before you can write any
有所了解。
programs. If you simply include a header file and use some
当在自己的程序中只是包含一个头文件并
functions or objects from that header, you'll probably get strange
应用此头文件的的函数或对象时,在编译程序时,可能会发生一些很奇怪的
sounding errors when you try to compile the program, to the effect
报错,这些报错就像编译器找不到指定头文件中声明一样。
that the compiler cannot find any of the declarations for the items
that you just included in the header file! After you see this message
在看了几次这样的报错后,那些意思也就明白了:“你是包含了头文件,但
a few times you'll become familiar with its meaning (which is “You
included the header file but all the declarations are within a
所有的声明都是在一个名字空间中,你也没有告诉编译器你想在当前的名字空间
namespace and you didn't tell the compiler that you wanted to use
里应用此声明”
the declarations in that namespace").

There's a keyword that allows you to say “I want to use the
在C++中另有一个关键字,我们用它就可以表明“我想在当前名字空间中
declarations and/or definitions in this namespace.” This keyword,
用此声明或(和)定义”
appropriately enough, is using. All of the Standard C++ libraries
这个关键字就是"using"。所有标准C++的库都包含在一个单一的名字空间是,这个名字
are wrapped in a single namespace, which is  std (for “standard”)
空间就是std(为standard之意)
As this book uses the standard libraries almost exclusively, you'll
由于此本书绝大多数情况下用的都是标准库函数,
see the following using directive in almost every program:
你会发现在此书几乎所有的程序中,都有这样的应用using namespace std;
using namespace std;
 
This means that you want to expose all the elements from the
它的意思是说,你想用到这个名为std的名字空间中的所有元素。
namespace called std. After this statement, you don't have to worry
在这样的声明后,由于在整个文件中已用了using,你就不必再为那个特定的
that your particular library component is inside a namespace, since
库在不在一个名字空间而担心了。 the  directive using makes that namespace available throughout the
file where the   directive was written.

原创粉丝点击