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

來源:互聯網
上載者:User

A. 標識範圍的層級        B. 標識成員屬於哪個類
C. 限定成員的作用範圍      D. 指出範圍的範圍

 

 

範圍符號::的前面一般是類名稱,後面一般是該類的成員名稱,C++為例避免不同的類有名稱相同的成員而採用範圍的方式進行區分
如:A,B表示兩個類,在A,B中都有成員member。那麼
      A::member就表示類A中的成員member
      B::member就表示類B中的成員member

 

 

 

全域範圍符號(雙冒號)例子

#include <stdio.h>

int count = 0; //全域變數 0

void main()
{
 
 int count = 10; //局部變數10
 
 printf("count =%d\n",count);//局部變數屏蔽了全域變數 所以輸出結果是10
 {
  int count = 20;
  
  printf("count =%d\n",::count); //範圍符號指出引用全域變數0
  //::sort
 }
}

 

 

 

 

範圍分辨符::有的時候單獨使用,代表什嗎?下面的代碼是在MFC中使用opengl
int nPixelFormat=::ChoosePixelFormat(m_hDC,&pfd);
::SetPixelFormat(m_hDC,nPixelFormat,&pfd);
m_hRC=::wglCreateContext(m_hDC);
::wglMakeCurrent(m_hDC,m_hRC);
請問,範圍分辨符有何含義,和不用範圍分辨符的有何分別?
(我嘗試了一下,沒發現有何區別) answer:如果名稱空間ns裡面有一個符號printf,
也有一個全域符號叫printf,
如果你用using namespace ns;匯入了ns的符號,這兩個printf就會衝突,當你只用printf會自動優先匹配ns::printf,所以要用::printf來特指全域符號裡面的printf.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.