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

前端 未结 6 786
青春惊慌失措
青春惊慌失措 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:37

    In general, when scopes are nested, any name declared in the inner scope hides any entities with the same name in an outer scope when that name is used in the inner scope. So, in this case, when used in the class scope, names declared in the class will hide those declared in the enclosing namespace.

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

提交回复
热议问题