Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scope resolution operator without a scope

Tags:

c++

In C++, what is the purpose of the scope resolution operator when used without a scope? For instance:

::foo(); 
like image 610
Landon Kuhn Avatar asked Sep 16 '08 18:09

Landon Kuhn


People also ask

How do you write a scope resolution operator?

The scope resolution operator is used to reference the global variable or member function that is out of scope. Therefore, we use the scope resolution operator to access the hidden variable or function of a program. The operator is represented as the double colon (::) symbol.

What is scope resolution operator with example?

The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class and used to access the static variables of class.

Which is the scope resolution operator?

The :: (scope resolution) operator is used to get hidden names due to variable scopes so that you can still use them.

What is the :: operator in C++?

In C++, scope resolution operator is ::. It is used for following purposes. 2) To define a function outside a class. 3) To access a class's static variables.


1 Answers

It means global scope. You might need to use this operator when you have conflicting functions or variables in the same scope and you need to use a global one. You might have something like:

void bar();    // this is a global function  class foo {     void some_func() { ::bar(); }    // this function is calling the global bar() and not the class version     void bar();                      // this is a class member }; 

If you need to call the global bar() function from within a class member function, you should use ::bar() to get to the global version of the function.

like image 192
Mark Avatar answered Sep 29 '22 10:09

Mark