Articles‎ > ‎Technical Articles‎ > ‎

Scope Resolution Operator In C++

posted Apr 12, 2010, 10:14 AM by Thiyagaraaj M   [ updated Aug 20, 2011, 4:27 AM ]
by
Thiyagaraj M
Jamuna Mary

Definition:

The scope resolution operator is use for the Unary scope operator , if a namespace scope (or) global Scope name is hidden by an explicit declaration of the Name in block or class.

Example:

int count=0;
int main(void)
{
int count=n;
::count=1;
count=2;
return 0;
}

The declaration of count is declared in the main function Hides the integer named count declared in global namespace scope.The statement :: count =1 accesses the variable named Count declared in global namespace scope.

Scope resolution operator in C++:

The scope resolution operator (::) in c++ used to Define the already declared in the member functions of the class.

C++ supports to the global variable from a function,Local variable is to defined in the same function name.

The syntax of the scope resolution operator:

:: global variable name

Resolution operator is placed between the front of the variable name then the global variable is affected.If no resolution operator is placed between the local variable is affected.

Example:

#include<iostream.h>
int n=12; //global variable
int main()
{
int n=13; //local variable
cout<<::n<<endl; //print global variable:12
cout<<n<<endl; //print the local variable:13
}

If the resolution operator is placed between the class name and the data member belonging the class then data name belonging to the particularly class is affected.

If it is place front of the variable name then the global variable is affected. It is no resolution operator is placed then the local variable is affected.