6

Possible Duplicate:
What is the meaning of prepended double colon “::” to class name?

I have been looking at a legacy C++ code and it had something like this:

::putenv(local_tz_char);
::tzset();

What does this syntax of prepending "::" to the function calls mean? Google-fu is failing me.

Community
  • 1
  • 1
DVK
  • 126,886
  • 32
  • 213
  • 327

6 Answers6

7

Also known as Scope resolution operator

In C++ is used to define the already declared member functions (in the header file with the .hpp or the .h extension) of a particular class. In the .cpp file one can define the usual global functions or the member functions of the class. To differentiate between the normal functions and the member functions of the class, one needs to use the scope resolution operator (::) in between the class name and the member function name i.e. ship::foo() where ship is a class and foo() is a member function of the class ship.

Example from Wikipedia:

#include <iostream>

// Without this using statement cout below would need to be std::cout
using namespace std; 

int n = 12; // A global variable

int main() {
  int n = 13; // A local variable
  cout << ::n << endl; // Print the global variable: 12
  cout << n   << endl; // Print the local variable: 13
}
sll
  • 61,540
  • 22
  • 104
  • 156
7

It means that the functions putenv() and tzset() will be looked up by the compiler in the global namespace.

Example

#include <iostream>

using namespace std;

//global function
void foo()
{
    cout << "This function will be called by bar()";
}

namespace lorem
{
    void foo()
    {
        cout << "This function will not be called by bar()";
    }

    void bar()
    {
        ::foo();
    }
}

int main()
{
    lorem::bar(); //will print "This function will be called by bar()"
    return 0;
}
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
3

There was a discussion yesterday (+ a year) on a similar question. Perhaps you can find a more indepth answer here.

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

Community
  • 1
  • 1
stanek
  • 582
  • 5
  • 15
2

It means: look up the function in the global namespace.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
1

It will use the specifically unqualified name (as opposed to anything imported with the using keyword).

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
0

the :: is the scope resolution operator, it tells the compiler in what scope to find the function.

For instance if you have a function with a local variable var and you have a global variable of the same name, you can choose to access the global one by prepending the scope resolution operator:

int var = 0;

void test() {
    int var = 5;
    cout << "Local: " << var << endl;
    cout << "Global: " << ::var << endl;
}

The IBM C++ compiler documentation puts it like this (source):

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.

The same can be done for methods inside a class and versions of the same name outside. If you wanted to access a variable, function or class in a specific namespace you could access it like this: <namespace>::<variable|function|class>

One thing to note though, even though it is an operator it is not one of the operators that can be overloaded.

Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50