隐藏与using声明
Effective c++ 中条款33中的疑问:
class Base {
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int);
virtual void mf2();
void mf3();
void mf3(double);
};
class Derived : public Base {
public:
using Base::mf1; // 让base class内为mf1和mf3的所有东西
using Base::mf3; //在Derived class作用域内都可见(并且public)
virtual void mf1();
void mf3();
void mf4();
};
Derived d;
int x;
d.mf1();
d.mf1(x);//现在没问题了,调用Base::mf1
d.mf2();
d.mf3();
d.mf3(x);//现在没问题了,调用Base::mf3
疑问在于: d.mf3()不会存在二义性吗?using将Base中的mf3在Derived中可见,那么在Derived可见作用域内将会出现重复定义的mf3(肯定不是这么回事,但是我实在找不到理解的方式,就是Derived::mf3()与Base::mf3()在名字查找过程中,是如何区分开的?),求解答,谢谢 c++ using 继承 隐藏
[解决办法]
标准强制要求的,c++11 7.3.3/15
When a using-declaration brings names from a base class into a derived class scope, member functions and member function templates in the derived class override and/or hide member functions and member function templates with the same name, parameter-type-list (8.3.5), cv-qualification, and ref-qualifier (if any) in a base class (rather than conflicting).
在主楼的例子里,虽然有 using Base::mf3,但是 Base::mf3 仍然不可见,因为 Derived::mf3 仍然 hide 它。