首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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)

二次

代码语言:javascript
复制
#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';
    }
}

二次

产出:

二次

代码语言:javascript
复制
Attempted a typeid of NULL pointer!

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券