When you call
foo();
C++ will search for something named foo
in the following order:
- Is there something with this name declared within the class?
- Is there something with this name in a base class?
- Is there something with that name in the namespace in which the class was declared? (And, if not, is there something with that name in the parent namespace of that namespace, etc.?)
- Finally, if nothing was found, is there something with that name in the global namespace?
On the other hand, writing
::foo();
will make C++ look for something purely in the global namespace.
If there is nothing named foo
in your class, any of its base classes, or any namespaces foo
was declared inside of, then there's no difference between the two approaches.
foo()
, unqualified name lookup is performed. With::foo()
, qualified name lookup is performed. Depending on surrounding context (not shown in example), the two may find the same or different entities. – Candiecandied