std::bad_cast
来自cppreference.com
|
|
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
| 在头文件 <typeinfo> 中定义
|
||
| class bad_cast : public std::exception; |
||
dynamic_cast为引用类型时,抛出这种类型的异常运行时检查(例如,因为继承的类型是不相关的)失败.
原文:
An exception of this type is thrown when a dynamic_cast to a reference type fails the run-time check (e.g. because the types are not related by inheritance).
目录 |
[编辑] 成员函数
| 构造一个新的bad_cast对象 (公共成员函数) | |
Inherited from std::exception
Member functions
| [虚]</div></div>
|
析构该异常对象 ( std::exception的公有虚成员函数)
|
| [虚]</div></div>
|
返回一个说明字符串 ( std::exception的公有虚成员函数)
|
</div>
[编辑] 示例
#include <iostream> #include <typeinfo> struct Foo { virtual void f() {} }; struct Bar { virtual void f() {} }; int main() { Bar b; try { Foo& f = dynamic_cast<Foo&>(b); } catch(const std::bad_cast& e) { std::cout << e.what() << '\n'; } }
输出:
Bad dynamic_cast!
