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

前端 未结 9 2155
鱼传尺愫
鱼传尺愫 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条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 16:26

    :: is the scope resolution operator. It's used to specify the scope of something.

    For example, :: alone is the global scope, outside all other namespaces.

    some::thing can be interpreted in any of the following ways:

    • some is a namespace (in the global scope, or an outer scope than the current one) and thing is a type, a function, an object or a nested namespace;
    • some is a class available in the current scope and thing is a member object, function or type of the some class;
    • in a class member function, some can be a base type of the current type (or the current type itself) and thing is then one member of this class, a type, function or object.

    You can also have nested scope, as in some::thing::bad. Here each name could be a type, an object or a namespace. In addition, the last one, bad, could also be a function. The others could not, since functions can't expose anything within their internal scope.

    So, back to your example, ::thing can be only something in the global scope: a type, a function, an object or a namespace.

    The way you use it suggests (used in a pointer declaration) that it's a type in the global scope.

    I hope this answer is complete and correct enough to help you understand scope resolution.

提交回复
热议问题