请教虚函数相关问题。
class Base1 {
public:
virtual void f() { cout << "Base1::f" << endl; }
};
class Base2 {
public:
virtual void f() { cout << "Base2::f" << endl; }
};
class Base3 {
public:
virtual void f() { cout << "Base3::f" << endl; }
};
class Derive:public Base1,public Base2,public Base3 {
void f1() { cout << "Derive::f1" << endl; }
};
Derive d;
Base1 *b1 = &d;//输出:Base1::f
Base2 *b2 = &d;//输出:Base2::f
Base3 *b3=&d;//输出:Base3::f
Base3 *b33 = reinterpret_cast<Base3*>(&d);//为什么会输出:Base1::f??
而将Derive改成
class Derive:public Base1,public Base2,public Base3 {
void f() { cout << "Derive::f1" << endl; }
};
以上语句就都输出为Derive::f1,为什么?
谢谢解答。