Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of prepended double colon "::"?

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

I also found:

typedef ::config::set ConfigSet; 
like image 833
rmbianchi Avatar asked Nov 24 '10 16:11

rmbianchi


People also ask

What does the double colon mean in C++?

Two colons (::) are used in C++ as a scope resolution operator. This operator gives you more freedom in naming your variables by letting you distinguish between variables with the same name.

What does colon mean in C++?

It's called an initialization list. It initializes members before the body of the constructor executes.

What does double colon mean in UML?

Package-name::Class-name. as the name string in the name compartment. A full pathname can be specified by chaining together package names separated by double colons (::). 4.3.3 Presentation options. Either or both of the attribute and operation compartments may be suppressed.


2 Answers

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.

like image 84
Wyatt Anderson Avatar answered Oct 01 '22 00:10

Wyatt Anderson


The :: operator is called the scope-resolution operator and does just that, it resolves scope. So, by prefixing a type-name with this, it tells your compiler to look in the global namespace for the type.

Example:

int count = 0;  int main(void) {   int count = 0;   ::count = 1;  // set global count to 1   count = 2;    // set local count to 2   return 0; } 
like image 22
Moo-Juice Avatar answered Oct 01 '22 00:10

Moo-Juice