VOOZH about

URL: https://www.geeksforgeeks.org/cpp/scope-resolution-operator-in-c/

⇱ Scope Resolution Operator in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Scope Resolution Operator in C++

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:


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.

Syntax

The scope resolution operator follows this general syntax:

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.


Output
3

Namespace Resolution

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


Output
10

Iterator Declaration

We use the scope resolution operator when declaring an iterator.


Output
1

Define Class Member Function Outside Class

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


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.


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.


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

Comment
Article Tags:
Article Tags: