我的基本问题是为什么名称隐藏在返回类型和参数列表都更改时不适用。请参考下面的样片。
// Example program
#include <iostream>
#include <string>
using namespace std;
class base {
public:
int f() const { cout <<"I am base void version. "<<endl; return 1;}
int f(string) const { cout <<"I am base string version. "<<endl; return 1;}
};
class Derived1 : public base {
public:
int f() const {
cout << "Derived1::f()\n";
return 2;
}
};
class Derived2 : public base {
public:
int f(int) const {
cout << "Derived2::f()\n";
return 3;
}
};
class Derived3 : public base {
public:
void f(int) const {
cout << "Derived3::f()\n";
}
};
int main()
{
string s("hello");
Derived1 d1;
int x = d1.f();
//d1.f(s); // string version hidden
Derived2 d2;
//x = d2.f(); // f() version hidden
x = d2.f(1);
Derived3 d3;
d3.f(1); // No name hiding
}
输出 :
Derived1::f()
Derived2::f()
Derived3::f()
在上述程序中
a) 为什么 Derived2 对象不隐藏字符串版本?
b) 为什么当返回类型和参数都匹配时名称隐藏不适用?
有关“名称隐藏在编译器级别如何工作”的任何链接或参考?很有用。
谢谢你。