C++中,关于 MI (多继承)一条不容争辩的事实是,MI 的出现就象打开了潘朵拉的盒子,带来了单继承中绝对不会存在的复杂性。其中,最基本的一条是二义性(参见条款 26) 。如果一个派生类从多个基类继承了一个成员名,所有对这个名字的访问都是二义的;你必须明确地说出你所指的是哪个成员。
class Lottery {
public:
virtual int draw();
...
};
class GraphicalObject {
public:
virtual int draw();
...
};
class LotterySimulation: public Lottery,
public GraphicalObject {
... // 没有声明 draw
};
LotterySimulation *pls = new LotterySimulation;
pls->draw(); // 错误! ---- 二义
pls->Lottery::draw(); // 正确
pls->GraphicalObject::draw(); // 正确
显式地限制修饰成员不仅很笨拙,而且还带来限制。
当显式地用一个类名来限制修饰一个虚函数时,函数的行为将不再具有虚拟的特征。相反,被调用的函数只能是你所指定的那个,即使调用是作用在派生类的对象上:
class SpecialLotterySimulation: public LotterySimulation {
public:
virtual int draw();
...
};
pls = new SpecialLotterySimulation;
pls->draw(); // 错误! ---- 还是有二义
pls->Lottery::draw(); // 调用 Lottery::draw
pls->GraphicalObject::draw(); // 调用 GraphicalObject::draw
注意,在这种情况下,
即使 pls(LotterySimulation*) 指向的是 SpecialLotterySimulation 对象,也无法(没有"向下转换" ---- 参见条款 39)调用这个类中定义的 draw 函数。