July 25th Wednesday (七月 二十五日 水曜日)
2007-08-04 17:25
337 查看
Traditional C++ Namespaces
The first term is declarative region. A declarative region is a region in which declarations can be made.
For example, you can declare a global variable outside of any function. The declarative region for that
variable is the file in which it is declared. If you declare a variable inside a function, its declarative
region is the innermost block in which it is declared.
The second term is potential scope. The potential scope for a variable begins at its point of declaration
and extends to the end of its declarative region. So the potential scope is more limited than the declarative
region because you can't use a variable above the point it is first defined.
A variable, however, might not be visible everywhere in its potential scope. For example, it can be hidden
by another variable of the same name declared in a nested declarative region. For example, a local variable
declared in a function (the declarative region is the function) hides a global variable declared in the same
file (the declarative region is the file). The portion of the program that actually can see the variable is
termed the scope, which is the way we've been using the term all along.
C++'s rules about global and local variables define a kind of namespace hierarchy. Each declarative region
can declare names that are independent of names declared in other declarative regions. A local variable declared
in one function doesn't conflict with a local variable declared in a second function.
New Namespace Features
What C++ now adds is the ability to create named namespaces by defining a new kind of declarative region,
one whose main purpose is to provide an area in which to declare names. The names in one namespace don't
conflict with the same names declared in other namespaces, and there are mechanisms for letting other parts
of a program use items declared in a namespace.
Namespaces can be located at the global level or inside other namespaces, but they cannot be placed in
a block. Thus, a name declared in a namespace has external linkage by default (unless it refers to a constant).
Namespaces are open, meaning you can add names to existing namespaces.
Using-Directive Versus Using-Declaration
Using a using-directive to import all the names wholesale is not the same as using multiple using-declarations.
It's more like the mass application of a scope resolution operator. When you use a using-declaration, it is as if
the name is declared at the location of the using- declaration. If a particular name already is declared in a function,
you can't import the same name with a using-declaration. When you use a using-directive, however, name resolution
takes place as if you declared the names in the smallest declarative region containing both the using-declaration
and the namespace itself.
Simply, "using namespace ..." is alike to put or duplicate the namespace in the smallest region. The namespace
is a entity, it includes various of declarations.
Generally speaking, the using-declaration is safer to use because it shows exactly what names you are making available.
And if the name conflicts with a local name, the compiler lets you know. The using-directive adds all names, even ones
you might not need. If a local name conflicts, it overrides the namespace version, and you won't be warned. Also,
the open nature of namespaces means that the complete list of names in a namespace might be spread over several locations,
making it difficult to know exactly which names you are adding.
Namespace can also nest declare, "using ..." or "using namespace ..." in the body of a namespace.
You can create an "unnamed namespace" by omitting the namespace name.
This behaves as if it were followed by a using-directive; that is, the names declared in this namespace are in potential
scope until the end of the declarative region containing the unnamed namespace. In this respect, they are like global variables.
However, because the namespace has no name, you can't explicitly use a using-directive or using-declaration to make the names
available elsewhere. In particular, you can't use names from an unnamed namespace in a file other than the one containing
the namespace declaration. This provides an alternative to using static variables with internal linkage. Indeed, the C++
standard deprecates the use of the keyword static in namespaces and global scope.
The first term is declarative region. A declarative region is a region in which declarations can be made.
For example, you can declare a global variable outside of any function. The declarative region for that
variable is the file in which it is declared. If you declare a variable inside a function, its declarative
region is the innermost block in which it is declared.
The second term is potential scope. The potential scope for a variable begins at its point of declaration
and extends to the end of its declarative region. So the potential scope is more limited than the declarative
region because you can't use a variable above the point it is first defined.
A variable, however, might not be visible everywhere in its potential scope. For example, it can be hidden
by another variable of the same name declared in a nested declarative region. For example, a local variable
declared in a function (the declarative region is the function) hides a global variable declared in the same
file (the declarative region is the file). The portion of the program that actually can see the variable is
termed the scope, which is the way we've been using the term all along.
C++'s rules about global and local variables define a kind of namespace hierarchy. Each declarative region
can declare names that are independent of names declared in other declarative regions. A local variable declared
in one function doesn't conflict with a local variable declared in a second function.
New Namespace Features
What C++ now adds is the ability to create named namespaces by defining a new kind of declarative region,
one whose main purpose is to provide an area in which to declare names. The names in one namespace don't
conflict with the same names declared in other namespaces, and there are mechanisms for letting other parts
of a program use items declared in a namespace.
Namespaces can be located at the global level or inside other namespaces, but they cannot be placed in
a block. Thus, a name declared in a namespace has external linkage by default (unless it refers to a constant).
Namespaces are open, meaning you can add names to existing namespaces.
Using-Directive Versus Using-Declaration
Using a using-directive to import all the names wholesale is not the same as using multiple using-declarations.
It's more like the mass application of a scope resolution operator. When you use a using-declaration, it is as if
the name is declared at the location of the using- declaration. If a particular name already is declared in a function,
you can't import the same name with a using-declaration. When you use a using-directive, however, name resolution
takes place as if you declared the names in the smallest declarative region containing both the using-declaration
and the namespace itself.
Simply, "using namespace ..." is alike to put or duplicate the namespace in the smallest region. The namespace
is a entity, it includes various of declarations.
Generally speaking, the using-declaration is safer to use because it shows exactly what names you are making available.
And if the name conflicts with a local name, the compiler lets you know. The using-directive adds all names, even ones
you might not need. If a local name conflicts, it overrides the namespace version, and you won't be warned. Also,
the open nature of namespaces means that the complete list of names in a namespace might be spread over several locations,
making it difficult to know exactly which names you are adding.
Namespace can also nest declare, "using ..." or "using namespace ..." in the body of a namespace.
You can create an "unnamed namespace" by omitting the namespace name.
This behaves as if it were followed by a using-directive; that is, the names declared in this namespace are in potential
scope until the end of the declarative region containing the unnamed namespace. In this respect, they are like global variables.
However, because the namespace has no name, you can't explicitly use a using-directive or using-declaration to make the names
available elsewhere. In particular, you can't use names from an unnamed namespace in a file other than the one containing
the namespace declaration. This provides an alternative to using static variables with internal linkage. Indeed, the C++
standard deprecates the use of the keyword static in namespaces and global scope.
相关文章推荐
- July 11th Wednesday (七月 十一日 水曜日)
- 2008 July 23th Wednesday (七月 二十三日 水曜日)
- February 25th Wednesday 2009 (二月 二十五日 水曜日)
- Aprial 25th Wednesday (四月 二十五日 水曜日)
- 2008 July 2nd Wednesday (七月 二日 水曜日)
- 2008 July 25th Friday (七月 二十五日 金曜日)
- July 8th Wednesday (七月 八日 水曜日)
- July 29th Wednesday (七月 二十九日 水曜日)
- July 4th Wednesday (七月 四日 水曜日)
- 2008 July 30th Wednesday (七月 三十日 水曜日)
- July 1st Wednesday (七月 一日 水曜日)
- July 22th Wednesday (七月 二十二日 水曜日)
- 2009 March 25th Wednesday (三月 二十五日 水曜日)
- July 18th Wednesday (七月 十八日 水曜日)
- 2008 July 9th Wednesday (七月 九日 水曜日)
- July 15th Wednesday (七月 十五日 水曜日)
- 2008 July 16th Wenesday (七月 十六日 水曜日)
- Februray 25th Sunday (一月 二十五日 日曜日)
- Aprial 18th Wednesday (四月 十八日 水曜日)
- June 25th Monday (六月 二十五日 月曜日)