Scope Resolution Operator
Occasionally, it is useful to distinguish between class member names and other names. The scope resolution operator (::) can be used here.
For example
class A
{
private:
int x;
float y;
public:
void readm() const;
/*
---------
---------
*/
};
In this class the full name (qualified name) of variable x is A::x. Similarly, the qualified name of y is A::y and the qualified name of readm() is A::readm().
Global Variables and Class Member Variables with the Same Name
What happens if a global variable and a class member use the same name?
For instance
int x; //global variable
int a;
class A //global x becomes hidden now
{
private:
int x;
public:
/*
--------
--------
*/
};
In the above code, as soon as a class defines a member with the same name as that of a global variable, the global variable becomes hidden. For instance,
int x;
int y;
class A
{
public:
int x; //global x is hidden now
void f(int i)
{
x=i; // Assigns i to A::x, not to global x
y=i; // OK. Assigns i to global y
}
};
Since x of A has the same name as that of a global variable, whenever x is used inside of the class and its functions, it refers to A::x. This is because x, the global one, is hidden now. However, if you still wan to access the global variable, you can use the scope resolution operator :: before the name. That is, if you write ::x, it refers to the global x.
To sum up, x and A::x refer to x of class A and ::x refers to the global variable.
int x;
int a;
class A
{
public:
int x; // global x is hidden now
void f(int i)
{
x=i; //assign i to A::x
::x=i; // assign i to global x
y=i; // assign i to global y i.e. to::y
}
};
You need to use :: for global variables if they are made hidden by a local variable with the same name, otherwise you needn’t use it. As you have seen, we haven’t used ::y for y as no other variable has hidden it.
Global Variables and Block Scope Variables with the Same Name
File scope items are normally hidden when you use the same name for an item within a block (class block, function block, or any other block), but you can uncover them with the :: operator.
int var;
void fc(int a)
{
int var = 2*a;
::var = var; // copies var(local one) to ::var(the global one).
//::var is different from var here.
}
Consider another example:
int x;
class A
{
public:
int x;
class B //nested class
{
void f(int j)
{
x = j; //assigns to A::x
::x = j; //assign to global x
nbsp; --------
--------
}
};
};
Accessing Local Names Hidden in an Inner Block
Note that :: without any prefix can be used to access names only in global scope. There is no way to access hidden local names.
For example,
void fc()
{
int var = 2;
{
int var;
::var =3; //error: cannot access var in enclosing non-global scope
//-------
}
}
Illustrative Program
/* Program to illustrate the functioning of the scope
* resolution operator
* http://www.byteguide.com
*/
#include <iostream>
using namespace std;
int a = 10; // global variable with file scope
int main()
{
int a = 15;
cout << "main() " << endl;
cout << "a = " << a << endl; // local a(=15)
cout << "::a = " << ::a << endl; // global a(=10)
{
int a = 25;
cout << "------------Inner Block------------" << endl;
cout << "a = " << a << endl; // local a(=25)
cout << "::a = " << ::a << endl; // global a(=10)
}
cout << "-----------Back to main()-----------" << endl;
cout << "a = " << a << endl; // local a(=15)
cout << "::a = " << ::a << endl; // global a(=10)
}
Output
main()
a = 15
::a = 10
------------Inner Block------------
a = 25
::a = 10
-----------Back to main()-----------
a = 15
::a = 10
Comments - No Responses to “Scope Resolution Operator”
Sorry but comments are closed at this time.