In C++, scope resolution operator is ::. It is used for following purposes.
1) To access a global variable when there is a local variable with same name:
Value of global x is 0 Value of local x is 10
2) To define a function outside a class.
3) To access a class’s static variables.
Value of static x is 1 Value of local x is 3 Test::y = 2;
4) In case of multiple Inheritance:
If same variable name exists in two ancestor classes, we can use scope resolution operator to distinguish.
A's x is 10 B's x is 20
5) For namespace
If a class having the same name exists inside two namespace we can use the namespace name with the scope resolution operator to refer that class without any conflicts
Here, cout and endl belong to the std namespace.
6) Refer to a class inside another class:
If a class exists inside another class we can use the nesting class to refer the nested class using the scope resolution operator
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Operands for sizeof operator
- Copy constructor vs assignment operator in C++
- Self assignment check in assignment operator
- Rules for operator overloading
- Result of comma operator as l-value in C and C++
- Comma operator should be used carefully
- C/C++ Ternary Operator - Some Interesting Observations
- Is assignment operator inherited?
- When should we write our own assignment operator in C++?
- Default Assignment Operator and References
- Can we use % operator on floating point numbers?
- Scope Resolution Operator Versus this pointer in C++?
- Set a variable without using Arithmetic, Relational or Conditional Operator
- Placement new operator in C++
- Operator Overloading in C++
- Overloading Subscript or array index operator  in C++
- Inserting elements in std::map (insert, emplace and operator )
- Scope of Variables in C++
- Overloading New and Delete operator in c++
- new vs operator new in C++
Improved By : ZK.