新闻  |   论坛  |   博客  |   在线研讨会
C++中双冒号的作用
电子禅石 | 2023-03-10 12:08:50    阅读:12779   发布文章

::是C++里的“作用域分解运算符”。比如声明了一个类A,类A里声明了一个成员函数voidf(),

但没有在类的声明里给出f的定义,那么在类外定义f时,就要写成voidA::f(),表示这个f()函数是类A的成员函数。

  :: 一般还有一种用法,就是直接用在全局函数前,表示是全局函数。当类的成员函数跟类外的一个全局函数同名时,

大提示在类内定义的时候,打此函数名默认调用的是本身的成员函数;如果要调用同名的全局函数时,就必须打上::以示区别。

比如在VC里,你可以在调用API函数时,在API函数名前加::。

 

 

 

 

 中的域区分符号(双冒号::)作用

A. 标识作用域的级别

B. 标识成员属于哪个类

C. 限定成员的作用范围

D. 指出作用域的范围

 

作用域符号::的前面一般是类名称,后面一般是该类的成员名称,C++为例避免不同的类有名称相同的成员而采用作用域的方式进行区分

如:A,B表示两个类,在A,B中都有成员member。那么

 A::member就表示类A中的成员member

 B::member就表示类B中的成员member 

 

 

 

 

 

 

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.

view plain

 

    :: 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.

 

 

view plain

 

    // 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:

view plain

 

    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.

view plain

 

    #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:

view plain

 

    #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  

    }  


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
属于自己的技术积累分享,成为嵌入式系统研发高手。
最近文章
签名类型
2024-04-29 16:28:59
cat 文件名
2024-04-29 15:05:34
推荐文章
最近访客