Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Information????

Status
Not open for further replies.

kaz

Technical User
Sep 26, 2000
10
0
0
CA
Can Anybody anybody explain in detail what the Scope Operator :: really mean.
Also if anybody knows of a web address that I can Visit
that will explain this concept, it would be very helpfull.
Also if you can show me a few examples it would be helpfull, Don't get me wrong, I have read about it and have a general idea but it gets confusing.

Thanks EveryBody
KAZ [sig][/sig]
 
The scope resolution operator :):) is used for when you declare a global variable and declare local variable of the same type and name in a function. If you want to use the global variable, you have to put the scope resolution operator before because the local variable overrides the global variable.

// example
#include <iostream.h>
int x; // global variable x

void main()
{
int x; // local variable x

cin >> x; // input local variable
cin >> ::x; // input global variable
}
 
The scope resolution operator also works in referencing certain local identities (such as static data members or functions of classes) as opposed to the global ones. So it goes in the other direction, too.


Code:
#include<iostream>
using namespace std;
int x;
class myClass {
public:
static int x;
}
x=5; // global
myClass::x=5; // local to myClass


So, the scope resolution operator can be seen as a tool to access OTHER areas of scope (i.e., accessing global from the local scope, or accessing local from the global scope).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top