为什么在下一个程序中成员函数foo优先于全局foo,尽管全局foo匹配类型?
#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();
}
Run Code Online (Sandbox Code Playgroud) 在下一个代码示例中:
#include <iostream>
using namespace std;
int f() {
return 0;
}
struct A {
int f() {
return 1; }
};
template<class T>
struct C : public T {
C() {
cout << f() << endl;
} };
int main() {
C<A> c; // prints 0
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我将继承更改为非模板,如下所示:
struct C : public A
然后它打印"1"而不是0.
这是为什么?