2011-05-11 84 views
9

考慮下面的代碼行:::沒有命名空間

::CGContextRef cgContext = cocoa::createCgBitmapContext(surface); 

爲什麼有沒有::之前指定的命名空間? 這是否意味着它使用與您所在類相同的命名空間?

回答

7

::指向全局命名空間。

3

::沒有任何名稱空間的名稱就意味着它之前是指全局命名空間

::CGContextRef cgContext = cocoa::createCgBitmapContext(surface); 

表示參考Global Namespace中的CGContextRef

8

:: in ::CGContextRef表示全局命名空間,表示CGContextRef在全局命名空間中定義。

int x = 10; 
namespace test 
{ 
    int x = 100; 
    void f() 
    { 
     std::cout << x << std::endl; //prints 100 
     std::cout << ::x << std::endl; //prints 10 
    }  
} 

在這裏看到完整的演示:http://www.ideone.com/LM8uo