std::bad_typeid
| Defined in header <typeinfo> |  |  | 
|---|---|---|
| class bad_typeid : public std::exception; |  |  | 
对象时引发此类型的异常。typeid运算符应用于多态类型的取消引用的空指针值。
二次
二次
继承图
成员函数
| (constructor) | constructs a new bad_typeid object (public member function) | 
|---|
继承自STD:例外
成员函数
| (destructor) virtual | destructs the exception object (virtual public member function of std::exception) | 
|---|---|
| what virtual | returns an explanatory string (virtual public member function of std::exception) | 
例
二次
#include <iostream>
#include <typeinfo>
 
struct S { // The type has to be polymorphic
    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!二次
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

