<1>如果父子类虚函数都有默认参数,这种情况下根据指针类型来调用对应函数
<2>构造过程中,虚表还没有建立,此时在构造或析构函数中调用虚函数为静态调用
<3>const类型函数和非const类型函数不构成覆盖(多态)
<4>返回类型不同不构成覆盖(多态),但可以为子类对象的指针或引用
通过虚表调用函数:
typedef void (*PFUNC)(void);
typedef PFUNC* PVTAB;
class A {
public:
virtual void foo (void) {
cout << "A::foo() invoked" << endl;
}
virtual void bar (void) {
cout << "A::bar() invoked" << endl;
}
};
class B : public A {
public:
void foo (void) {
cout << "B::foo() invoked" << endl;
}
};
int main (int argc, char* argv[]) {
A a;
PVTAB pVtab = *(PVTAB*)&a;
cout << "A::VTAB[0] = " << (void*)pVtab[0] << endl;
(pVtab[0]) ();
cout << "A::VTAB[1] = " << (void*)pVtab[1] << endl;
(pVtab[1]) ();
B b;
pVtab = *(PVTAB*)&b;
cout << "B::VTAB[0] = " << (void*)pVtab[0] << endl;
(pVtab[0]) ();
cout << "B::VTAB[1] = " << (void*)pVtab[1] << endl;
(pVtab[1]) ();
return 0;
}
结果:
A::VTAB[0] = 00401087
A::foo() invoked
A::VTAB[1] = 00401267
A::bar() invoked
B::VTAB[0] = 00401082
B::foo() invoked
B::VTAB[1] = 00401267
A::bar() invoked