std::bad_typeid

定义于头文件 <typeinfo>
class bad_typeid : public std::exception;

此类型的异常在应用 typeid 运算符到多态类型的空指针值时抛出。

cpp/error/exceptionstd-bad typeid-inheritance.svg
关于这幅图像

继承图

目录

成员函数

构造新的 bad_typeid 对象
(公开成员函数)

继承自 std::exception

成员函数

析构该异常对象
(std::exception 的虚公开成员函数)
[虚]
返回解释性字符串
(std::exception 的虚公开成员函数)

示例

#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!

版本历史

  • (当前 | 先前 2017年3月30日 (四) 04:31Fruderica讨论 | 贡献. . (1,048字节) (-257). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:12P12bot讨论 | 贡献 . . (1,305字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:38P12bot讨论 | 贡献 . . (1,305字节) (-15). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 17:22P12bot讨论 | 贡献 . . (1,320字节) (+225). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 11:35P12讨论 | 贡献 . . (1,095字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,095字节) (-23). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:33P12讨论 | 贡献 . . (1,118字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,118字节) (+1,118). . (Translated from the English version using Google Translate)