下面的代码:
class A1 {
public:
int x;
};
class A2 {
private:
int x() { return 67; }
};
class M : public A1, public A2 {};
int main() {
M m;
m.x;
}
编译有错误:
error C2385: ambiguous access of 'x'
note: could be the 'x' in base 'A1'
note: or could be the 'x' in base 'A2'
但为什么?仅有的A1::x
M 应该可见。A2::x
应该是纯本地的。
In C++, 姓名查询发生在之前会员访问检查被执行。因此,名称查找(在您的情况下是不合格的)找到两个名称,这是不明确的。
您可以使用限定名称来消除歧义:
int main() {
M m;
m.A1::x; //qualifed name-lookup
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)