Scope resolution operator (::) is used to define member variables and function outside of the class and it is used to set and access the global variable and static data member of a class.
- Define function outside the class using scope resolution operator.
double Box::getVolume() {
return length * breadth * height;
}
- Initialize and access of static data member of class.
class X
{
public:
static int count;
};
int X::count = 10; // define static data member
int main ()
{
cout << X::count << endl; // use static member of class X
}
- Set and Access the global variable.
int count = 0;
void main() {
int count = 0;
::count = 1; // set global count to 1
count = 2; // set local count to 2
cout << "Global count : " << ::count <<endl;
cout << "Local count : " << count <<endl;
}