前置双冒号“::"是什么意思?

我在一个必须修改的类中发现了这行代码:

I found this line of a code in a class which I have to modify:

::Configuration * tmpCo = m_configurationDB;//pointer to current db

而且我不知道类名前面的双冒号到底是什么意思.没有它,我会读到:tmpCo 的声明作为指向 Configuration 类的对象的指针......但是前置的双冒号让我感到困惑.

and I don't know what exactly means the double colon prepended to the class name. Without that I would read: declaration of tmpCo as a pointer to an object of the class Configuration... but the prepended double colon confuses me.

我还发现:

typedef ::config::set ConfigSet;

推荐答案

这可确保从全局命名空间开始解析,而不是从您当前所在的命名空间开始.例如,如果您有两个名为 <代码>配置如下:

This ensures that resolution occurs from the global namespace, instead of starting at the namespace you're currently in. For instance, if you had two different classes called Configuration as such:

class Configuration; // class 1, in global namespace
namespace MyApp
{
    class Configuration; // class 2, different from class 1
    function blah()
    {
        // resolves to MyApp::Configuration, class 2
        Configuration::doStuff(...) 
        // resolves to top-level Configuration, class 1
        ::Configuration::doStuff(...)
    }
}

基本上,它允许您遍历全局命名空间,因为您的名称可能会被另一个命名空间内的新定义破坏,在本例中为 MyApp.

Basically, it allows you to traverse up to the global namespace since your name might get clobbered by a new definition inside another namespace, in this case MyApp.

相关文章