What is the meaning of prepended double colon "::"?
Asked Answered
H

9

525

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;
Housetop answered 24/11, 2010 at 16:24 Comment(1)
Don't really feel it is an answer, so I'll comment: en.wikipedia.org/wiki/Scope_resolution_operator. In this context, the naked :: means reference the variable from the global/anonymous namespace.Kieffer
K
625

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.

Kiri answered 24/11, 2010 at 16:27 Comment(6)
What is the reason for putting 2 sets of double colons? In this: ::Configuration::doStuff(...)Hypersthene
@NoniA. are you asking what the second set of double colons does?Clueless
@WyattAnderson, no the 1st set. I think I understand that the :: in-between two terms refers to the namespace or class and its member. But what about the 1st one?Hypersthene
@Hypersthene that's what OP asks, that's the question this post answers. It makes sure to use identifier from global namespace. Look at the example againTephra
Why not name the class differently in namespace App to avoid confusion? Why do they have to have the same name?Merrilee
@AbelTom They don't have to have the same name, and you should name them differently. The feature only exists in case there ever happens to be a niche case where it is needed.Fiber
S
247

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;
}
Soso answered 24/11, 2010 at 16:27 Comment(1)
I had no idea about this, now it makes me feel powerful, thank you!Lynnalynne
C
158

Lots of reasonable answers already. I'll chip in with an analogy that may help some readers. :: works a lot like the filesystem directory separator '/', when searching your path for a program you'd like to run. Consider:

/path/to/executable

This is very explicit - only an executable at that exact location in the filesystem tree can match this specification, irrespective of the PATH in effect. Similarly...

::std::cout

...is equally explicit in the C++ namespace "tree".

Contrasting with such absolute paths, you can configure good UNIX shells (e.g. zsh) to resolve relative paths under your current directory or any element in your PATH environment variable, so if PATH=/usr/bin:/usr/local/bin, and you were "in" /tmp, then...

X11/xterm

...would happily run /tmp/X11/xterm if found, else /usr/bin/X11/xterm, else /usr/local/bin/X11/xterm. Similarly, say you were in a namespace called X, and had a "using namespace Y" in effect, then...

std::cout

...could be found in any of ::X::std::cout, ::std::cout, ::Y::std::cout, and possibly other places due to argument-dependent lookup (ADL, aka Koenig lookup). So, only ::std::cout is really explicit about exactly which object you mean, but luckily nobody in their right mind would ever create their own class/struct or namespace called "std", nor anything called "cout", so in practice using only std::cout is fine.

Noteworthy differences:

1) shells tend to use the first match using the ordering in PATH, whereas C++ gives a compiler error when you've been ambiguous.

2) In C++, names without any leading scope can be matched in the current namespace, while most UNIX shells only do that if you put . in the PATH.

3) C++ always searches the global namespace (like having / implicitly your PATH).

General discussion on namespaces and explicitness of symbols

Using absolute ::abc::def::... "paths" can sometimes be useful to isolate you from any other namespaces you're using, part of but don't really have control over the content of, or even other libraries that your library's client code also uses. On the other hand, it also couples you more tightly to the existing "absolute" location of the symbol, and you miss the advantages of implicit matching in namespaces: less coupling, easier mobility of code between namespaces, and more concise, readable source code.

As with many things, it's a balancing act. The C++ Standard puts lots of identifiers under std:: that are less "unique" than cout, that programmers might use for something completely different in their code (e.g. merge, includes, fill, generate, exchange, queue, toupper, max). Two unrelated non-Standard libraries have a far higher chance of using the same identifiers as the authors are generally un- or less-aware of each other. And libraries - including the C++ Standard library - change their symbols over time. All this potentially creates ambiguity when recompiling old code, particularly when there's been heavy use of using namespaces: the worst thing you can do in this space is allow using namespaces in headers to escape the headers' scopes, such that an arbitrarily large amount of direct and indirect client code is unable to make their own decisions about which namespaces to use and how to manage ambiguities.

So, a leading :: is one tool in the C++ programmer's toolbox to actively disambiguate a known clash, and/or eliminate the possibility of future ambiguity....

Centrum answered 24/11, 2010 at 16:42 Comment(3)
+1 for good analogy. analogies aren't used nearly enough IMO as a teaching tool.Expert
Excellent, very clear answer.Haith
Another good analogy is with domain names - example.org. is unambiguously rooted in the global namespace, unlike example.org. But I guess some readers are less familiar with that.Biramous
A
55

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

Armagnac answered 24/11, 2010 at 16:43 Comment(2)
@obounaim Consider this code liveworkspace.org/code/3Wabw0$5 class some { protected: int thing; }; class some_ext : public some { float thing; void action(){ some::thing = 42; thing = 666; } }; Here some is a base class of some_ext and when you write some::thing into member functions of some_ext, it means the thing object into the base type some. Without some::, thing alone means the thing in the closest scope, that is some_ext::thing. Is it more clear?Armagnac
my apology for resurrecting a comment from a decade ago but that link seems to have fallen into a phishing siteRhapsodist
D
23

:: is used to link something ( a variable, a function, a class, a typedef etc...) to a namespace, or to a class.

if there is no left hand side before ::, then it underlines the fact you are using the global namespace.

e.g.:

::doMyGlobalFunction();

Devinne answered 24/11, 2010 at 16:29 Comment(0)
B
13

its called scope resolution operator, A hidden global name can be referred to using the scope resolution operator ::
For example;

int x;
void f2()
{
   int x = 1; // hide global x
   ::x = 2; // assign to global x
   x = 2; // assign to local x
   // ...
}
Butt answered 11/10, 2013 at 17:59 Comment(0)
L
12

(This answer is mostly for googlers, because OP has solved his problem already.) The meaning of prepended :: - scope resulution operator - has been described in other answers, but I'd like to add why people are using it.

The meaning is "take name from global namespace, not anything else". But why would this need to be spelled explicitly?

Use case - namespace clash

When you have the same name in global namespace and in local/nested namespace, the local one will be used. So if you want the global one, prepend it with ::. This case was described in @Wyatt Anderson's answer, plese see his example.

Use case - emphasise non-member function

When you are writing a member function (a method), calls to other member function and calls to non-member (free) functions look alike:

class A {
   void DoSomething() {
      m_counter=0;
      ...
      Twist(data); 
      ...
      Bend(data);
      ...
      if(m_counter>0) exit(0);
   }
   int m_couner;
   ...
}

But it might happen that Twist is a sister member function of class A, and Bend is a free function. That is, Twist can use and modify m_couner and Bend cannot. So if you want to ensure that m_counter remains 0, you have to check Twist, but you don't need to check Bend.

So to make this stand out more clearly, one can either write this->Twist to show the reader that Twist is a member function or write ::Bend to show that Bend is free. Or both. This is very useful when you are doing or planning a refactoring.

Lathery answered 23/10, 2013 at 13:4 Comment(0)
C
7

:: is a operator of defining the namespace.

For example, if you want to use cout without mentioning using namespace std; in your code you write this:

std::cout << "test";

When no namespace is mentioned, that it is said that class belongs to global namespace.

Centrobaric answered 24/11, 2010 at 16:26 Comment(0)
D
0

"::" represents scope resolution operator. Functions/methods which have same name can be defined in two different classes. To access the methods of a particular class scope resolution operator is used.

Dishearten answered 20/2, 2019 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.