C++运行时获取类型信息的type_info类与bad_typeid异常


type_info 类
type_info 类描述编译器在程序中生成的类型信息。此类的对象可以有效存储指向类型的名称的指针。 type_info 类还可存储适合比较两个类型是否相等或比较其排列顺序的编码值。类型的编码规则和排列顺序是未指定的,并且可能因程序而异。
必须包含 <typeinfo> 标头文件才能使用 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_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 类似于 address-of (&) 运算符。
type_info::name 成员函数可将 const char* 返回到以 null 结尾的字符串,该字符串表示类型的用户可读名称。将缓存所指向的内存,应该从不直接释放它。
type_info::raw_name 成员函数可将 const char* 返回到以 null 结尾的字符串,该字符串表示对象类型的修饰名称。该名称实际上以其修饰的形式存储以节省空间。因此,此函数比 type_info::name 更快,因为它不需要取消修饰名称。 type_info::raw_name 函数返回的字符串在比较运算符中很有用,但它不可读。如果您需要用户可读的字符串,请改用 type_info::name 函数。

bad_typeid 异常
当 typeid 的操作数是 Null 指针时,typeid 运算符将引发 bad_typeid 异常。
语法

  catch (bad_typeid)
statement

备注
bad_typeid 的接口为:

class bad_typeid : public exception
{
public:
  bad_typeid(const char * _Message = "bad typeid");
  bad_typeid(const bad_typeid &);
  virtual ~bad_typeid();
};

以下示例演示引发 bad_typeid 异常的 typeid 运算符。

// expre_bad_typeid.cpp
// compile with: /EHsc /GR
#include <typeinfo.h>
#include <iostream>

class A{
public:
  // object for class needs vtable
  // for RTTI
  virtual ~A();
};

using namespace std;
int main() {
A* a = NULL;

try {
  cout << typeid(*a).name() << endl; // Error condition
  }
catch (bad_typeid){
  cout << "Object is NULL" << endl;
  }
}

输出

Object is NULL


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3