WEB开发网
开发学院软件开发C++ C++中运行时类型信息(RTTI)及动态强制类型转换 阅读

C++中运行时类型信息(RTTI)及动态强制类型转换

 2012-08-21 12:49:24 来源:WEB开发网   
核心提示://typeid函数定义在typeinfo头文件中,typeid()接收一个类型对象,C++中运行时类型信息(RTTI)及动态强制类型转换(2),此函数返回一个typeinfo对象,调用typeinfo对象的name方法,即可以打印传入的类型对象的具体类型信息cout<<"Now Object =
//typeid函数定义在typeinfo头文件中,typeid()接收一个类型对象,此函数返回一个typeinfo对象,调用typeinfo对象的name方法,即可以打印传入的类型对象的具体类型信息
cout<<"Now Object = "<<typeid(*employee[i]).name()<<"is destrctor !!!"<<endl;//返回相应的typeinfo对象
delete employee[i];//动态释放相应内存
}
}
void ptrPrint(const Employee const *employee){//指向常量数据的常量指针
employee->print();
cout<<"\nearning = "<<employee->earning()<<endl;//静态调用所有方法employee->earning();
}
=================================================
程序执行的结果:
CommsionEmployee: bai, sunwu
socialNumber : 123456789 SalaryRate = 0.50, sailSalary = 10000.00
Earning = 5000.00
Old baseCommsionEmployee sala = 10000.00//此处动态的识别了BaseCommsionEmployee对象类型,动态强制类型转换,并修改了相应的信息。
After increame 10% ,Now baseCommsionEmployee baseSala = 11000.00
BaseSalary CommsionEmployee: bai, ruru
socialNumber : 987654321 SalaryRate = 0.50, sailSalary = 10000.00 BaseSalar
1000.00
Earning = 16000.00
WeekEmployee: bai, sun
socialNumber : 192837465 weekSalary = 4000.00
Earning = 4000.00
HourEmployee: bai, wu
socialNumber : 00000000 hours = 8.00 hourSalary = 100.00
Earning = 800.00
Now Object = class CommsionEmployeeis destrctor !!!//动态类型识别
Now Object = class BaseCommsionEmployeeis destrctor !!!
Now Object = class WeekEmployeeis destrctor !!!
Now Object = class HourlyEmployeeis destrctor !!!

上一页  1 2 

Tags:中运 行时 类型

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接