没有命名空间

17

考虑以下代码行:

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

为什么在 :: 前面没有指定命名空间呢? 这是不是意味着它使用了你所在的类相同的命名空间?

3个回答

23

::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


8
::指的是全局命名空间。

5

:: 在没有命名空间的情况下使用,表示它引用 全局命名空间

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

意思是在全局命名空间中引用 CGContextRef


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接