What is the meaning of prepended double colon “::”?

前端 未结 9 2138
鱼传尺愫
鱼传尺愫 2020-11-22 15:54

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

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

and I do

9条回答
  •  Happy的楠姐
    2020-11-22 16:19

    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(...)
        }
    }
    

    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.

提交回复
热议问题