C ++ calls the same function with the scope operator - is this helpful?
I know that operator :: in C ++ is scope resolution, but what's the purpose of calling a function inside a class with it like this
class MyClass
{
int myFunc(int argument)
{
// do some stuff
return (::myFunc(another_argument));
}
}
is there a practical reason? Is "this" suspicious of this?
+3
source to share
1 answer
If you had a use case:
//in the global namespace
int myFunc(int);
//elsewhere
class MyClass
{
int myFunc(int argument)
{
// do some stuff
return (::myFunc(another_argument));
}
}
Here we need to distinguish between a member function and a free function. This is a fairly common occurrence when packaging C libraries.
In this case, it ::
forces compilation to choose a version that is in the global namespace rather than a member function that will eventually recursively call itself.
+9
source to share