您的位置:首页 > 编程语言 > C语言/C++

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.

:: 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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: