What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

What is Scope Resolution Operator in C++? (Why It is Used?)

  • Apr 20, 2023
  • 5 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Abrar Ahmed
What is Scope Resolution Operator in C++? (Why It is Used?)

C++ is powerful for writing complex code efficiently. One such great feature it offers is the ability to control the "scope" of the variables. It refers to a region of a program where a particular variable can be accessed. In this article, we will explore what is a scope resolution operator in C++ and how it is used.

But first, we should revise the basics of Scopes.

What are the different scopes in C++?

The scope is the control mechanism to ensure that no leakage in data happens as well as prevent unauthorized access within the code. In C++, variables and functions can exist in different types of scope across the code. These scopes dictate from which part of the scope can we access which variable.

The different scopes in C++ are:

  • Global scope: Variables and functions that exist outside of any function or class have global scope. This means that they can be accessed from anywhere within the code.
  • Function scope: Variables and functions that exist within a function have function scope. This means that they can only be accessed from within that function. 
    Block scope: Variables that are defined within a block of code have block scope. This means that they can only be accessed from within that block. 
  • Class scope: Variables and functions that are defined within a class have class scope. They can be accessed only from within any member function of that class. 

Scopes are important because they allow programmers to control how variables and functions are accessed within the code. This can help to prevent naming conflicts and improve code readability. This can also help to reduce bugs and make the code easier to maintain.

What is a Scope Resolution Operator in C++?

We have seen a lot of information about the scopes in C++. But how can we implement different scopes or manipulate them? This is done using the scope resolution operator.

The scope resolution operator is a unique operator that is used to access variables and functions that exist within a specific scope. In C++, the scope resolution operator is represented by two colons (::) and is placed in front of the name of the variable or function that is being accessed.

The scope resolution operator helps us in understanding the scope of variables and functions. It helps us adjust the scope to make sure our code works as we want it to.  Here is an example:

#include <iostream>
using namespace std;

int x = 5; // global variable

int main() {
int x = 10; // local variable
cout << "Local x: " << x << endl;
cout << "Global x: " << ::x << endl;
return 0;
}

 

Output:

Local x: 10
Global x: 5


In this example, we have defined a global variable called x and a local variable called x within the main() function. When we use the scope resolution operator (::x), we can access the global x variable from within the main() function. This allows us to differentiate between the global and local variables that have the same name.

What is the use of a Scope Resolution Operator?

The scope resolution operator is used to access variables and functions that exist within a specific scope. The scope resolution operator is very useful when two variables in different scopes have the same name.

For example, if we wanted to access a global variable inside a function that has the same name as a local variable in the function. We would use the scope resolution operator as shown in the code above to access the global variable. By using the scope resolution operator, we can specify which variable or function we want to access, regardless of its scope.

Another common use of the scope resolution operator is in object-oriented programming. In C++, objects are created from classes, and the scope resolution operator is used to access member functions and variables of those objects. For example:

#include <iostream>
using namespace std;

class MyClass {
public:
int x;
void printX() {
cout << "X is: " << x << endl;
}
};

int main() {
MyClass obj;
obj.x = 5;
obj.printX();
return 0;
}

 

Output:

X is: 5

 

In this example, we have defined a class called MyClass, which contains a public integer variable called x and a public member function called printX(). We then create an object of the MyClass class called obj and set its x value to 5. We can then use the scope resolution operator (obj.printX()) to access the printX() member function of the obj object, which will output the value of x.

This operator can also be used to specify the class to which a function or variable belongs. In this way, we can avoid naming conflicts with other functions or variables in the same scope. Even when overloading functions, the scope resolution operator can specify which overloaded function to call, based on the parameters passed to the function. 

Conclusion

In summary, the scope resolution operator is an important tool in C++ that is useful in situations where there are multiple variables or functions with the same name, but in different scopes. Happy Learning :)

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abrar Ahmed
An ambivert individual with a thirst for knowledge and passion to achieve. Striving to connect Artificial Intelligence in all aspects of life. I am also an avid coder and partake in coding challenges all the time on Leetcode and CodeChef.