父类能动态调子类的虚函数么?
如题。想通过父类的构造函数,调用到子类的hit函数,打印出son hit,不知可能不可能?
class father
{
public:
father()
{
cout<<"fasther init"<<endl;
hit();
}
~father()
{
cout<<"fasther delete"<<endl;
}
virtual void hit()
{
cout<<"father hit"<<endl;
}
};
class son : public father
{
public:
son()
{
cout<<"son init"<<endl;
}
~son()
{
cout<<"son delete"<<endl;
}
virtual void hit()
{
cout<<"son hit"<<endl;
}
};
void main()
{
son* myson = new son();
delete myson;
myson = NULL;
}