Scope resolution operator (::)
In C++ programming language, it is used
-to define a function outside a class or
-when we want to use a global variable but also has a local variable with the same name.
Syntax with function:
return_Type class_name :: function_name()
{
}
Syntax to access global variable:
::global_varible
Scope resolution operator with global variable:
program:
#include< iostream>
using namespace std;
//global variable
char c = 'a';
int main(){
//local variable
char c = 'b';
cout << "Local variable: " << c << "n";
//using scope resolution operator
cout << "Global variable: " << ::c << "n";
return 0;
}
Local variable: b
Global variable: a
#include< iostream>
using namespace std;
class Myclass
{
public:
void function();
};
void Myclass::function()
{
cout<<"This is my function outside class";
}
int main()
{
Myclass obj;
obj.function();
return 0;
}
This is my function outside class