- 论坛徽章:
- 0
|
超菜问题
错。
这个不是为了加快编译速度,而是为了方便协同开发而增加的关键字。
其中包括要对2各关键字的理解 namespace 和 using.
我也不是完全了解,因为我从来没有用过。我的理解是c++已经
有class对对象进行了包装,但是没有对以前用文件进行管理的
全局变量,公共函数进行包装。namespace用来完成这个任务。
下面是我从c++builder中截出的解释,英文好的可以帮助翻译一下。
我算然看的懂,但是让我给你讲出来就啰嗦的要死。
namespace
Category
C++-Specific Keywords
Description
Most nontrivial applications consist of more than one source file. The files can be authored and maintained by more than one developer. Eventually, the separate files are organized and linked to produce the final application. Traditionally, the file organization requires that all names that aren't encapsulated within a defined namespace (such as function or class body, or translation unit) must share the same global namespace. Therefore, multiple definitions of names discovered while linking separate modules require some way to distinguish each name. The solution to the problem of name clashes in the global scope is provided by the C++ namespace mechanism.
The namespace mechanism allows an application to be partitioned into a number of subsystems. Each subsystem can define and operate within its own scope. Each developer is free to introduce whatever identifiers are convenient within a subsystem without worrying about whether such identifiers are being used by someone else. The subsystem scope is known throughout the application by a unique identifier.
It only takes two steps to use C++ namespaces. The first is to uniquely identify a namespace with the keyword namespace. The second is to access the elements of an identified namespace by applying the using keyword.
using (declaration)
Category
C++-Specific Keywords
Description
You can access namespace members individually with the using-declaration syntax. When you make a using declaration, you add the declared identifier to the local namespace. The grammar is
using-declaration:
using :: unqualified-identifier;
Using declaration
If you want to use several (or all of) the members of a namespace, C++ provides an easy way to get access to the complete namespace. The using-directive specifies that all identifiers in a namespace are in scope at the point that the using-directive statement is made. The grammar for the using-directive is as follows.
using-directive:
using namespace :: opt nested-name-specifier opt namespace-name;
The using-directive is transitive. That means that when you apply the using directive to a namespace that contains using directives within itself, you get access to those namespaces as well. For example, if you apply the using directive in your program, you also get namespaces A, ONE, and TWO.
namespace A {
using namespace ONE; // This has been defined previously
using namespace TWO; // This also has been defined previously
}
The using-directive does not add any identifiers to your local scope. Therefore, an identifier defined in more than one namespace won't be a problem until you actually attempt to use it. Local scope declarations take precedence by hiding all other similar declarations.
补充:
这个在c++老版本上没有(bc31)。gcc上不知道有没有,知道的请说一声。
新标准不一定就必须使用,如果你只编写windows下的程序估计没有问题。
如果要跨平台那么为了方便移植最好使用大家通用认可的标准。 |
|