Scope Resolution Operator In C++
On this page (8sections)
Definition
The scope resolution operator is used for the Unary scope operator if a namespace scope (or) Global Scope
Scope resolution operator in C++
- In C++ language the scope resolution operator is written ”::”.
- C++ supports to the global variable from a function, Local variable is to define the same function name.
- Identify variables with use of scope resolution operator when we use the same name for local variable and global variable ( or in class or namespace )
- Resolution operator is placed between the front of the variable name then the global variable is affected.If no resolution operator is placed between the local variable is affected.
Scope Resolution Operator Syntax
:: identifier // for Global Scope
class-name :: identifier // for Class Scope
namespace :: identifier // for Namespace Scope
//simple syntax
:: global variable name
Understanding Scope Resolution Operator
int data = 0;
int main(void) {
int data = n;
::data= 5;
data = 10;
return 0;
}
We have declared two ‘data’ variable in above program. One is a global variable. the second one is in the main function.The declaration of the ‘data’ is declared in the main function Hides the integer named ‘data’ declared in global namespace scope.If we can access with scope resolution operator like ::data.
The statement ::data=5 accesses the variable named ‘data’ declared in global namespace scope.
Scope Resolution Operator Example Program: Global Scope
// Example program for Scope Resolution OperatorGlobal Variable
#include<iostream>
int n = 12; //global variable
int main() {
int n = 13; //local variable
cout << ::n << endl; //print global variable:12
cout << n << endl; //print the local variable:13
}
12
13
If the resolution operator is placed between the class name and the data member belonging the class then data name belonging to the particular class is affected.
If it is placed the front of the variable name then the global variable is affected. It is no resolution operator is placed then the local variable is affected.
Scope Resolution Operator Example Program: Namscape Scope
#include <iostream>
using namespace std;
//Namespace namespace first
namespace n1 {
int x = 100;
}
//Namespace namespace second
namespace n2 {
double x = 200;
}
int main() {
//Namespace namespace n1 Usage
cout << "n1 value : " << n1::x << endl;
//n1::x Holds 100
//Namespace namespace n2 Usage
cout << "n2 value : " << n2::x << endl;
//n::x Holds 200
return 0;
}
100
200
Scope Resolution Operator Example Program: For Function
/* Simple scope resolution operator Example for Function defined in C++*/
#include <iostream>
using namespace std;
class ScopeFn {
public:
void print(); //function declaration
};
// Function defined using scope resolution operator outside the class
void ScopeFn::print() {
cout << "Function defined using scope resolution operator.\n";
}
int main() {
ScopeFn obj;
obj.print();
return 0;
}
Function defined using scope resolution operator
Related Pages
- Home — Browse all Home.
- Input Output Formats In C++ — More in Blog.
- Polymorphism In C++ — More in Blog.
- Data Hiding and Encapsulation using Access Specifiers in C++ — More in Blog.