<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>
"); //-->

博客專(zhuān)欄

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

C++中雙冒號的作用

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

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

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

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

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

比如在VC里,你可以在調用API函數時(shí),在A(yíng)PI函數名前加::。

 

 

 

 

 中的域區分符號(雙冒號::)作用

A. 標識作用域的級別

B. 標識成員屬于哪個(gè)類(lèi)

C. 限定成員的作用范圍

D. 指出作用域的范圍

 

作用域符號::的前面一般是類(lèi)名稱(chēng),后面一般是該類(lèi)的成員名稱(chēng),C++為例避免不同的類(lèi)有名稱(chēng)相同的成員而采用作用域的方式進(jìn)行區分

如:A,B表示兩個(gè)類(lèi),在A(yíng),B中都有成員member。那么

 A::member就表示類(lèi)A中的成員member

 B::member就表示類(lèi)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++ 雙冒號作用域轉換操作符

使用雙冒號操作符 (::) 可以用以下名稱(chēng)限定具有全局作用域的 C++ 成員函數、頂級函數或變量:

 

    重載名(不同參數類(lèi)型使用同一名稱(chēng))

    二義名(不同類(lèi)中使用同一名稱(chēng)) 

 

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  

    }  


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



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

相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>