C++ typeinfo 库 - bad_typeid



描述

它在空指针的 typeid 上抛出异常。

声明

以下是 std::bad_typeid 的声明。

C++98

	
class bad_typeid;

C++11

class bad_typeid;

参数

返回值

异常

不抛出异常保证 - 此成员函数从不抛出异常。

数据竞争

区域设置对象被修改。

示例

以下为 std::bad_typeid 的示例。

#include <iostream>
#include <typeinfo>
 
struct S {
   virtual void f();
};

int main() {
   S* p = nullptr;
   try {
      std::cout << typeid(*p).name() << '\n';
   } catch(const std::bad_typeid& e) {
      std::cout << e.what() << '\n';
   }
}

输出应如下所示:

Attempted a typeid of NULL pointer!
typeinfo.htm
广告
© . All rights reserved.