C++提供了运行时类型检测(Run-Time Type Identification)来在运行期获得对象的实际派生类型
在实际的运用上,可以通过不同的派生类转换基类引用统一操作(如传参)等,节省函数重载次数
如类Base 类Deri : public Base
Deri d;
Base& b = d;
通过使用一个基类引用派生类,之后使用基类引用即可通过typeid来获取run-time type
如:typeid(b).name() 实际输出为“class Deri”
或是用指针:
Deri d;
Base* b = &d;
std::cout<<typeid(*b).name(); //实际输出class Deri
记之备忘