Scope operatur function call

Hello,

I have seen the following case (see below).
What does it mean? Why can showResult be called with
the :: operator? Why is it done that way?

showResult is defined in an other cpp file than
it is called from.

Thanks for help.


cpp A:

bool ClassA::showResult() const
{
return ::showResult();
}


.h: (A)
virtual bool showResult() const;

cpp B:
bool showResult()
{


When the scope resolution operator :: is used as a unary prefix operator, the left-hand side is assumed to be the global scope. For example, std::cout and ::std::cout are equivalent. It's generally only used when the name in the global scope is shadowed by a name in a more local scope, as in your example above where showResult in ClassA wants to call out to the global scope function of the same name. Without the :: it would just be infinite recursion because it would call itself.
Last edited on
:: is the scope resolution operator.
If there is nothing on the left hand side of the ::, it refers to something declared in the global namespace, the global function showResult in your case.
Without the ::, it would have referred to the local (class) function.
In case you have functions (or variables, or classes, etc.) with the same name, the compiler chooses the right function to call following the name lookup rules, see http://en.cppreference.com/w/cpp/language/lookup
Topic archived. No new replies allowed.