Scope Resolution Operator in C++
In C++, the scope resolution operator (::) is used to access the identifiers such as variable names and function names defined inside some other scope in the current scope. Let’s take a look at an example:
#include <iostream>
int main() {
// Accessing cout from std namespace using scope
// resolution operator
std::cout << "GeeksforGeeks";
return 0;
}
Output
GeeksforGeeks
Explanation: The std namespace contains the declaration of cout. So, to use cout, we first need to tell the compiler that it is declared inside the std namespace which is done using ::. The compiler then resolves the cout from there.
Table of Content
Syntax of Scope Resolution Operator
The scope resolution operator follows this general syntax:
scope_name :: identifier
where scope_name is the name of the scope where identifier is defined.
Applications of Scope Resolution Operator
Following are the main applications of scope resolution operator illustrated with an example:
Accessing Global Variables
When a local variable shadows a global variable, we can use :: to access the global variable.
#include <iostream>
using namespace std;
// Global x
int x = 3;
int main() {
// Local x
int x = 10;
// Printing the global x
cout << ::x;
return 0;
}
Output
3
Namespace Resolution
It is also used to resolve the identifier declared inside different namespaces.
#include <bits/stdc++.h>
using namespace std;
// A sample namespace with a variable
namespace N {
int val = 10;
};
int main() {
// Accessing val from namespace N
cout << N::val;
return 0;
}
Output
10
Define Class Member Function Outside Class
It is also used to define the member function of the class outside the class template.
#include <iostream>
using namespace std;
// A sample class
class A {
public:
// Only declaration of member function
void fun();
};
// Definition outside class by referring to it
// using ::
void A::fun() {
cout << "fun() called";
}
int main() {
A a;
a.fun();
return 0;
}
Output
fun() called
Access Class’s Static Members
Static members of a class can be accessed without creating the object of the class. It is possible to access them using scope resolution operator.
#include<iostream>
using namespace std;
class A {
public:
static int x;
};
// In C++, static members must be explicitly defined
// like this
int A::x = 1;
int main() {
// Accessing static data member
cout << A::x;
return 0;
}
Output
1
Refer to Base Class Member in Derived Class
The scope resolution operator can also be used to refer to the members of base class in a derived class especially if they have the same name.
#include <bits/stdc++.h>
using namespace std;
class Base {
public:
void func() {
cout << "Base class func()" << endl;
}
};
class Derived : public Base {
public:
// Overridden function
void func() {
cout << "Derived class func()" << endl;
}
};
int main() {
Derived obj;
// Calling base class's func() from the object of
// derived class
obj.Base::func();
obj.func();
return 0;
}
Output
Base class func() Derived class func()