#include <iostream>
using namespace std;
class employee{
public:
virtual void salary(){
cout<<"employee salary"<<endl;
}
};
class programmer:public employee{
public:
void salary()
{
cout<<"programmer salary"<<endl;
}
void bonus()
{
cout<<"programmer bonus"<<endl;
}
};
void payroll(employee *pe)
{
programmer *pm=dynamic_cast<programmer*>(pe);
if(pe)
{
pm->bonus();
}
else
{
pe->salary();
}
}
int main()
{
employee *pe=new programmer;
payroll(pe);
}
在VC下运行,总是抛出异常。
project--> C/C++-->project option 加上/GR
靠RTTI实现的,不过VC默认情况下是关掉了RTTI的,因为RTTI会有运行时的消耗,
影响效率......