pankaj
pankaj

Reputation: 355

what does "::'variable'" refer to?

what does ::i mean in the below program?

#include<iostream>
using namespace std;
int i = 10;
int main(){
   int i = 20;
   {
   int i = 30;
   cout << i << ::i <<endl;
   }
}

It outputs "3010". "::i" seems to be referring to the global declaration. What does it mean exactly?

Upvotes: 4

Views: 273

Answers (8)

Bitmap
Bitmap

Reputation: 12538

::i means search for i which is not nested in anything. In your case this will be refering to i=10; Also see c/c++ search wildcard and syntax.

Upvotes: 0

useraged
useraged

Reputation: 1716

Variables and functions which starts with :: operator describes that this variable or function is in the global namespace (not local). In your example i means local variable which defined at 7th line and ::i means global one which defined at 3rd line. But i does not refer to outer local variable which is defined at 5th line because it's redefined and inaccessible in that scope (line 6 to 9).

Upvotes: 4

Alok Save
Alok Save

Reputation: 206616

It is called as Qualified Name lookup.
It referes to the variable i declared at global scope.
Simply it means don't refer the local variable i if any but refer the i in global scope.

Whenever you have same named variables in the local scope, When referring to the variable name the preference is given to the variable in the local scope. This means in your program, the i inside the local scope { } hides all the other variables named i namely the ones(i=20 as well as i=10). So to refer the i, which is in the global scope, you use the :: scope resolution operator.=

Upvotes: 9

Matteo Italia
Matteo Italia

Reputation: 126917

The :: operator is the scope resolution operator. When you write scopename::name it means that you are referring to the object/function/... named name into the scope scopename (usually a namespace); e.g., if you didn't write using namespace std; to use cin/cout/... you would have to write std::cin/std::cout/...

If the scope resolution operator isn't preceded by a scope name, it means that you're explicitly referring to an object put into the global namespace, as the global i is in your program.

This is particularly useful if you have currently imported several namespaces (using the using namespace directive) or have local names that hide the global ones. In your code, this is exactly what happens: the local i "hides" the global one, and you need the :: to access the global i.

Random tip: you'll often see :: used to refer to global identifiers when you're dealing with C libraries (that have no notion of namespaces) and you want to be sure you're calling a particular C function instead of some C++ method that hides it into the current scope (this happens a lot in MFC code that wants to call Windows APIs).

Upvotes: 3

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361702

::i refers to the global variable.

:: is used for global namespace. So ::i means the variable i declared at global namespace.

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 191037

It is just a scoping operator. It is generally used for namespaces.

Upvotes: 0

user802003
user802003

Reputation:

::i refers to the i in the global namespace.

Upvotes: 0

Mahesh
Mahesh

Reputation: 34625

i at the global namespace.

{
  int i = 30;
  cout << i << ::i <<endl;

  // 1. First i refers to the block scoped i having value 30
  // 2. ::i refers to the global variable i having the value 10
}

Upvotes: 0

Related Questions