C++中双冒号作用域解析符的含义
2010-03-16 16:43
471 查看
先把各家的解释复制在这里,意思都差不多。
Visual C++ Language Reference
Scope Resolution Operator: ::
You can tell the compiler to use the global identifier rather than the local identifier by prefixing the identifier with ::, the scope resolution operator.
Remarks
The identifier can be a variable or a function.
If you have nested local scopes, the scope resolution operator does not provide access to identifiers in the next outermost scope. It provides access to only the global identifiers.
Example
This example has two variables named amount. The first is global and contains the value 123. The second is local to the main function. The scope resolution operator tells the compiler to use the global amount instead of the local one.
IBM XL C/C++ for AIX, V10.1
Scope resolution operator :: (C++ only)
The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example:
The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope.
You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator.
In the following example, the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator.
Sun Studio 12: dbx
C++ 双冒号作用域转换操作符
使用双冒号操作符 (::) 可以用以下名称限定具有全局作用域的 C++ 成员函数、顶级函数或变量:
重载名(不同参数类型使用同一名称)
二义名(不同类中使用同一名称)
Wiki:
Visual C++ Language Reference
Scope Resolution Operator: ::
You can tell the compiler to use the global identifier rather than the local identifier by prefixing the identifier with ::, the scope resolution operator.
:: identifier class-name :: identifier namespace :: identifier
Remarks
The identifier can be a variable or a function.
If you have nested local scopes, the scope resolution operator does not provide access to identifiers in the next outermost scope. It provides access to only the global identifiers.
Example
This example has two variables named amount. The first is global and contains the value 123. The second is local to the main function. The scope resolution operator tells the compiler to use the global amount instead of the local one.
// expre_ScopeResolutionOperator.cpp // compile with: /EHsc // Demonstrate scope resolution operator #include <iostream> using namespace std; int amount = 123; // A global variable int main() { int amount = 456; // A local variable cout << ::amount << endl // Print the global variable << amount << endl; // Print the local variable }
IBM XL C/C++ for AIX, V10.1
Scope resolution operator :: (C++ only)
The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class. For example:
int count = 0; int main(void) { int count = 0; ::count = 1; // set global count to 1 count = 2; // set local count to 2 return 0; }
The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope.
You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator.
In the following example, the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator.
#include <iostream> using namespace std; class X { public: static int count; }; int X::count = 10; // define static data member int main () { int X = 0; // hides class type X cout << X::count << endl; // use static member of class X }
Sun Studio 12: dbx
C++ 双冒号作用域转换操作符
使用双冒号操作符 (::) 可以用以下名称限定具有全局作用域的 C++ 成员函数、顶级函数或变量:
重载名(不同参数类型使用同一名称)
二义名(不同类中使用同一名称)
Wiki:
#include <iostream> using namespace std; int n = 12; // A global variable int main() { int n = 13; // A local variable cout << ::n << endl; // Print the global variable: 12 cout << n << endl; // Print the local variable: 13 }
相关文章推荐
- c++中的system("pause")的作用和含义解析
- C++中双冒号的作用
- C/C++学习 ---- void及void指针含义的深刻解析
- ANROID JNI中一些C++文件,冒号和双冒号作用
- C++ “::” 作用域符 双冒号
- C++命名空间 namespace的作用和使用解析
- C++命名空间 namespace的作用和使用解析
- C++中双冒号(::)和单冒号(:)的区别和作用
- C++命名空间 namespace的作用和使用解析
- C++命名空间 namespace的作用和使用解析
- C++命名空间 namespace的作用和使用解析
- C++ 单冒号: 和双冒号:: 的作用
- C++命名空间 namespace的作用和使用解析
- [转]c++ virtual public的含义和作用
- Host文件的含义和作用(快速解析与屏蔽域名)
- C++ 构造函数后面的冒号的作用
- C/C++中,空数组、空类、类中空数组的解析及其作用。
- 转:C/C++中,空数组、空类、类中空数组的解析及其作用
- C++中的双冒号作用
- C++之构造函数后面:冒号的作用