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

std::bad_cast

Defined in header <typeinfo>

?

?

class bad_cast : public std::exception;

?

?

对象时引发此类型的异常。dynamic_cast对于引用类型,运行时检查%28e.g失败。因为类型与继承%29无关,而且std::use_facet如果请求的方面不存在于区域设置中。

二次

二次

继承图

成员函数

(constructor)

constructs a new bad_cast 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 Foo { virtual ~Foo() {} };
struct Bar { virtual ~Bar() {} };
 
int main()
{
    Bar b;
    try {
        Foo& f = dynamic_cast<Foo&>(b);
    } catch(const std::bad_cast& e)
    {
        std::cout << e.what() << '\n';
    }
}

二次

可能的产出:

二次

代码语言:javascript
复制
Bad dynamic cast

二次

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com