C ++: why a member function takes precedence over a global function

Why in the next program does the member function foo take precedence over the global foo, although the global one is of type?

#include <iostream> using namespace std; void foo(double val) { cout << "double\n";} class obj { public: void callFoo() { foo(6.4); } private: void foo(int val) {cout << "class member foo\n"; } }; int main() { obj o; o.callFoo(); } 
+7
c ++ function-overloading
source share
6 answers

Think about what happens if a global function declared somewhere in your code base (possibly several #include statements) exceeds a member function of a class obj declared right there in the class itself ...

This would mean that if you want to play it safely, you will have to fully qualify every call to a member function ...

 this->foo(); 

... instead of qualifying a less likely case of an actual call to a global function.

 ::foo(); 

This is called the "concept of least surprise."

+7
source share

Since a member function named foo can be found in the class scope, and then the name search will stop, so the global version of foo will not be considered to overload resolution , even if the global version is more appropriate here. This kind of name is hiding .

If you want to call the global version, you can explicitly call it ::foo(6.4); .

And here is a workaround for casting a global function into overload resolution .

Link for Unqualified Name Search

+9
source share

One of the principles of method overloading is that the concrete must surpass the general. In your example, the member method will always be more specific than the global method.

+3
source share

In general, when regions are nested, any name declared in the inner region hides any objects with the same name in the outer region when this name is used in the inner region. Thus, in this case, when used in a class scope, names declared in the class will hide those that are declared in the enclosing namespace.

The external name is still available if you qualify it; in this case, being in the global namespace, it is available as ::foo .

+3
source share

You might think of it as increasing “specialization” when moving from a global namespace through any nested namespaces around your class, nested classes, etc. up to the member functions involved. If you say “foo” in this internal context, a more “specialized” foo may be more appropriate for the caller. Also, if global was used preferentially, then someone adding characters to a less specialized area might break the class that defined the same character, overriding the many benefits of namespaces and scope. The main idea is that your class has great freedom to use meaningful, but compressed identifiers that can be used by other code in your application, but this is only when the value for this identifier is not available in the local area itself, which it starts to search further and further.

+1
source share

In terms of priority for region resolution (default) when using the same identifier name, local scope, class member (scope class), global scope

+1
source share

All Articles