type_info 类
type_info 类描述编译器在程序中生成的类型信息。 此类的对象可以有效存储指向类型的名称的指针。 type_info 类还可存储适合比较两个类型是否相等或比较其排列顺序的编码值。 类型的编码规则和排列顺序是未指定的,并且可能因程序而异。
必须包含 <typeinfo>
头文件才能使用 type_info 类。 type_info 类的接口是:
class type_info {
public:
type_info(const type_info& rhs) = delete; // cannot be copied
virtual ~type_info();
size_t hash_code() const;
_CRTIMP_PURE bool operator==(const type_info& rhs) const;
type_info& operator=(const type_info& rhs) = delete; // cannot be copied
_CRTIMP_PURE bool operator!=(const type_info& rhs) const;
_CRTIMP_PURE int before(const type_info& rhs) const;
size_t hash_code() const noexcept;
_CRTIMP_PURE const char* name() const;
_CRTIMP_PURE const char* raw_name() const;
};
不能直接实例化 type_info 类的对象,因为该类只有一个私有复制构造函数。 构造(临时)type_info 对象的唯一方式是使用 typeid 运算符。 由于赋值运算符也是专用的,因此不能复制或给类 type_info 的对象赋值。
type_info::hash_code
可定义适合将 typeinfo 类型的值映射到索引值的分布的哈希函数。
运算符 ==
和 !=
分别用于与其他 type_info 对象比较是否相等和不相等。
类型的排列顺序与继承关系之间没有关联。 使用 type_info::before
成员函数可确定类型的排序。 不能保证 type_info::before
在不同的程序中(甚至是多次运行同一程序时)会产生相同的结果。 通过这种方式, type_info::before
类似于 (&)
运算符的地址。
type_info::name
成员函数可将 const char*
返回到以 null 结尾的字符串,该字符串表示类型的用户可读名称。 将缓存所指向的内存,应该从不直接释放它。
type_info::raw_name
成员函数特定于 Microsoft。 它将 const char*
返回到以 null 结尾的字符串,该字符串表示对象类型的修饰名称。 该名称以其修饰的形式存储以节省空间。 因此,此函数比 type_info::name
更快,因为它不需要取消修饰名称。 type_info::raw_name
函数返回的字符串在比较运算符中很有用,但它不可读。 如果需要用户可读的字符串,请改用 type_info::name
。
仅当指定了 /GR(启用运行时类型信息)编译器选项时,才会为多态类生成类型信息。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈