::没有命名空间

pwa*_*ang 11 c++ namespaces

考虑以下代码行:

::CGContextRef cgContext = cocoa::createCgBitmapContext( surface );
Run Code Online (Sandbox Code Playgroud)

为什么在::之前没有指定名称空间?这是否意味着它使用与您所在类相同的命名空间?

Naw*_*waz 9

::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
    }     
}
Run Code Online (Sandbox Code Playgroud)

请在此处查看完整演示:http://www.ideone.com/LM8uo


Pep*_*epe 7

::指的是全局命名空间.


Alo*_*ave 5

::在它之前没有任何命名空间名称意味着它指的是Global Namespace

::CGContextRef cgContext = cocoa::createCgBitmapContext( surface );
Run Code Online (Sandbox Code Playgroud)

表示CGContextRef在全局命名空间中引用。