关于bad_typeid异常

什么是bad_typeid异常?

当typeid运算符应用于多态类型的已取消引用的空指针值时,将引发此类型的异常。

继承关系:

class bad_typeid : public exception

例子:

// expre_bad_typeid.cpp
// compile with: /EHsc /GR
#include <typeinfo>
#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;
   }
}

备注

对空指针进行typeid时引发此异常

 

posted on 2020-11-06 07:53  活着的虫子  阅读(366)  评论(0编辑  收藏  举报

导航