Never call Virtual functions during construction or destruction。
因为在构造函数中调用Virtual函数,会导致继承类不能正常调用其多态函数,因为这个时候先是初始化的基类。
class base
{
public:
base()
{
method();
}
virtual void method()
{
cout<<"this is the base method"<<endl;
}
};
class derived : public base
{
public:
void method(){
cout<<"this is the derived method"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
derived A; //调用的是 base method
base* B = &A;
B->method();
getchar();
return 0;
}