In the function called ::foo()
I don't understand what the syntax is for.
If it was foo::count_all()
then I know that count_all
is a function of class or namespace foo
.
In the case of ::foo()
what is the ::
referencing?
The ::
operator is calling a namespace
or class
. In your case it is calling the global namespace which is everything not in a named namespace.
The example below illustrates why namespaces are important. If you just call foo()
your call can't be resolved because there are 2 foo
s. You need to resolve the global one with ::foo()
.
namespace Hidden {
int foo();
}
int foo();
using namespace Hidden; // This makes calls to just foo ambiguous.
int main() {
::foo(); // Call to the global foo
hidden::foo(); // Call to the foo in namespace hidden
}
::
with nothing before it indicates the global namespace. eg:
int foo(); // A global function declaration
int main() {
::foo(); // Calling foo from the global namespace.
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With