bad_cast
描述 (Description)
这是动态转换失败时抛出的异常。
声明 (Declaration)
以下是std :: bad_cast的声明。
class bad_cast;
C++11
class bad_cast;
参数 (Parameters)
没有
返回值 (Return Value)
没有
异常 (Exceptions)
No-throw guarantee - 没有成员抛出异常。
例子 (Example)
在下面的示例中为std :: bad_cast。
#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';
}
}
样本输出应该是这样的 -
std::bad_cast