目录

bad_cast

描述 (Description)

它会在动态转换失败时引发异常。

声明 (Declaration)

以下是std :: bad_cast的声明。

C++98

	
class bad_cast;

C++11

class bad_cast;

参数 (Parameters)

没有

返回值 (Return Value)

没有

异常 (Exceptions)

No-throw guarantee - 此成员函数永远不会抛出异常。

数据竞争 (Data races)

区域设置对象已修改。

例子 (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
↑回到顶部↑
WIKI教程 @2018