Scope Resolution Operator(::) in C++
In this tutorial, we will learn about the scope resolution operator in C++. The scope resolution operator(::) is very useful in many cases. We will see a detailed description of those uses here.
Scope Resolution Operator in C++
scope resolution operator(::) is used to access elements that cannot be accessed directly due to limitation in their scope. Here are some of the common uses of the scope resolution operator in C++.
Namespaces
Sometimes we may have two classes with the same name in two different namespaces. In such cases, we need to use a scope resolution operator to tell the compiler which namespace to use. See the below code for a better understanding.
#include <iostream> int main() { //using namespace std for cout and endl objects std::cout<<"Hi there!"<<std::endl; return 0; }
Output:
Hi there!
We can also write the above code by writing ”using namespace std;” before the main() function instead of writing as in the example. The output will still be the same.
To know more about namespaces: Namespaces in C++
Accessing a global variable when there is already a local variable with the same name
When there are a global variable and a local variable with the same name, the local variable is given priority in its scope. However, we can access the global variable using the scope resolution operator. See the code.
#include <iostream> using namespace std; int a=12; int main() { int a=9; cout<<"local a is: "<<a<<endl; cout<<"global a is: "<<::a<<endl; return 0; }
Output:
local a is: 9 global a is: 12
Defining a function outside a class
We can also define a function outside a class that has been declared inside the class using scope resolution operator. See the code below.
#include <iostream> using namespace std; class c { public: void f(); //function declaration }; //function definition void c::f() { cout<<"Hi there!"<<endl; } int main() { //creaating object of class c c c_object; //calling funciton f() c_object.f(); return 0; }
Output:
Hi there!
Multiple inheritance
When there are variables with the same in two different classes and we inherit those classes in a child class then we can use scope resolution operator to specify the variable we are trying to access. You may check this: Multiple inheritance in C++.
See the example code given here.
#include <iostream> using namespace std; class c1 { protected : int a; public : c1() { a=4; } }; class c2 { protected : int a; public : c2() { a=5; } }; class c3 : public c1, public c2 { public : void f() { cout<<"value of a from c1 is "<<c1::a<<endl; cout<<"value of a from c2 is "<<c2::a<<endl; } }; int main() { //creaating object of class c3 c3 c3_object; //calling funciton f() c3_object.f(); return 0; }
Output:
value of a from c1 is 4 value of a from c2 is 5
Accessing static variable of a class
We can also use scope resolution operator to access static variables of a class. This is very well illustrated in the example.
#include <iostream> using namespace std; class c { static int a; public : //function that returns sum of local variable and the static variable. int f(int a) { return a+c::a; } }; //we must define static variable like this in C++ int c::a=10; int main() { //creaating object of class c3 c c1_object; int a=5; //calling funciton f() int result=c1_object.f(a); cout<<"function returns "<<result; return 0; }
Output:
function returns 15
Class inside another class:
When there is a class inside another class we can access members of the inside class using scope resolution operator. See the example.
#include <iostream> using namespace std; class out { public : class in { public : static int a; }; }; int out::in::a=5; int main() { //creating object of class in out::in in_object; cout<<"a is "<<in_object.a; return 0; }
Output:
a is 5
Thank you.
I like it! Easy to understand for a beginner. continue updating more!
Thanks much!