Scope Resolution Operator in C++ (original) (raw)

Last Updated : 23 Jul, 2025

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:

C++ `

#include

int main() {

  // Accessing cout from std namespace using scope
  // resolution operator
std::cout << "GeeksforGeeks";

return 0;

}

`

**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.

Syntax

The scope resolution operator follows this general syntax:

C++ `

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, meaning both have the same name, we can use the **scope resolution operator :: to access the global variable.

CPP `

#include using namespace std;

// Global x int x = 3;

int main() {

  // Local x
int x = 10;

  // Printing the global
  // variable x
cout << ::x;
return 0;

}

`

Namespace Resolution

It is also used to access the identifier such variables, functions and classes declared inside **namespaces.

C++ `

#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;

}

`

Iterator Declaration

We use the scope resolution operator when declaring an **iterator.

C++ `

#include <bits/stdc++.h> using namespace std;

int main() { vector arr = {1, 2, 3, 4, 5};

// Defining an iterator pointing to
// the beginning of the vector
vector<int>::iterator first =
arr.begin();
  
cout << *first;
return 0;

}

`

Define Class Member Function Outside Class

The scope resolution operator :: allows us to define a member function of a class outside the **class definition.

C++ `

#include 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; }

`

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.

C++ `

#include 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;

}

`

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.

C++ `

#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()

Can Scope Resolution Operator be Overloaded?

Since the scope resolution operator (::) is important in how C++ handles variables, functions and classes, it cannot be overloaded. When the program is compiled, the compiler needs to know the purpose of every name it sees. Unlike other operators, :: lets you access global variables, classes and namespaces. If programmers could customize ::, it would bring much confusion and make the overall language approximately not secure and simple. Therefore, the scope resolution operator cannot be overloaded in C++.