C++: Why member function has priority over global function

前端 未结 6 761
青春惊慌失措
青春惊慌失措 2020-12-17 21:14

Why in the next program the member function foo has priority over the global foo although the global one match the type?

#include 
using name         


        
6条回答
  •  眼角桃花
    2020-12-17 21:53

    Because the member function named foo could be found at the class scope, and then name lookup will stop, so the global version foo won't be considered for overload resolution, even if the global version is more appropriate here. It is a kind of name hiding.

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

    And here is a workaround to bring the global function into overload resolution.

    Reference for Unqualified name lookup

提交回复
热议问题