博客專欄

EEPW首頁(yè) > 博客 > C++中雙冒號(hào)的作用

C++中雙冒號(hào)的作用

發(fā)布人:電子禪石 時(shí)間:2023-03-10 來(lái)源:工程師 發(fā)布文章

::是C++里的“作用域分解運(yùn)算符”。比如聲明了一個(gè)類A,類A里聲明了一個(gè)成員函數(shù)voidf(),

但沒(méi)有在類的聲明里給出f的定義,那么在類外定義f時(shí),就要寫(xiě)成voidA::f(),表示這個(gè)f()函數(shù)是類A的成員函數(shù)。

  :: 一般還有一種用法,就是直接用在全局函數(shù)前,表示是全局函數(shù)。當(dāng)類的成員函數(shù)跟類外的一個(gè)全局函數(shù)同名時(shí),

大提示在類內(nèi)定義的時(shí)候,打此函數(shù)名默認(rèn)調(diào)用的是本身的成員函數(shù);如果要調(diào)用同名的全局函數(shù)時(shí),就必須打上::以示區(qū)別。

比如在VC里,你可以在調(diào)用API函數(shù)時(shí),在API函數(shù)名前加::。

 

 

 

 

 中的域區(qū)分符號(hào)(雙冒號(hào)::)作用

A. 標(biāo)識(shí)作用域的級(jí)別

B. 標(biāo)識(shí)成員屬于哪個(gè)類

C. 限定成員的作用范圍

D. 指出作用域的范圍

 

作用域符號(hào)::的前面一般是類名稱,后面一般是該類的成員名稱,C++為例避免不同的類有名稱相同的成員而采用作用域的方式進(jìn)行區(qū)分

如:A,B表示兩個(gè)類,在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++ 雙冒號(hào)作用域轉(zhuǎn)換操作符

使用雙冒號(hào)操作符 (::) 可以用以下名稱限定具有全局作用域的 C++ 成員函數(shù)、頂級(jí)函數(shù)或變量:

 

    重載名(不同參數(shù)類型使用同一名稱)

    二義名(不同類中使用同一名稱) 

 

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  

    }  


*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。



關(guān)鍵詞: c++

相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉