RTTI运行时类型识别
RTTI是Runtime Type Identification的缩写,是“运行时类型识别”的意思。面向对象的编程语言,象C++, Java ,Delphi都提供了对RTTI的支持。 本文将简略介绍 RTTI 的一些背景知识、描述 RTTI 的概念,并通过具体例子和代码介绍什么时候使用以及如何使用 RTTI。本文还将详细描述两个重要的 RTTI 运算符的使用方法,它们是 typeid 和dynamic_cast。 1. typeid的用法 1.1type_info类 typeid 的结果是 const type_info&。所以下面先对type_info类作下介绍: class type_info { public: virtual ~type_info(); size_t hash_code() const _CRTIMP_PURE bool operator==(const type_info& rhs) const; _CRTIMP_PURE bool operator!=(const type_info& rhs) const; _CRTIMP_PURE int before(const type_info& rhs) const; _CRTIMP_PURE const char* name() const; _CRTIMP_PURE const char* raw