‘::’ Unary operator
The scope operator is used to refer to member of global namespace between the global name space does have the name, the rotation ::
- Member name refers to a member of the global namespace.
- This can be useful for referring to members of global namespace whose names have been hidden by names declared in nested local scope. Unless we specify to the compiler in which namespace to search for a declaration, the compiler simple searches the current scope and any scopes in which the current scope is nested, to find the declaration for the name
Namespace is a collection of
- variables
- Functions
- Classes
namespace N
{
Classes
}
int x; global variable
void main (); global name space
{ int x=10;
int y =20; void main()
cout <<y; - > 20 {
cout<<x; - > 20 int x=20;
} cout<<x; -> 20
cout<< :: x;
cout<<N :: x;
- global name space - > unnamed
Unary operator in C++ with Example
# include <iostream.h>
int x=10;
void main()
{
int y=20;
cout<< “\n local variable y=” <<y; 20
cout<<”\n global variable u=”<<x;10
cout<<”\n global variable x=”<< :: x;
}
Example
# include <iostream.h>
{
int x=10;
int y=10;
void main()
{
int z=30;
int x=40;
{
cout<< end l<< x; // 50
cout<< end l << :: x; //10
cout<<end l << z; //30
cout<< end l<<y; // 20
cout<< end l<<::y;
}
cout << endl<<x; // 40
cout << endl << Z; //30
cout << endl<<:: x; //20
coout<<endl<<::y; //20
}