C++ typeinfo 庫 - bad_cast



描述

如果動態轉換失敗,它會丟擲一個異常。

宣告

以下是 std::bad_cast 的宣告。

C++98

	
class bad_cast;

C++11

class bad_cast;

引數

返回值

異常

不丟擲保證 - 此成員函式從不丟擲異常。

資料競爭

區域設定物件被修改。

示例

在下面的 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&&gt;(b);
   } catch(const std::bad_cast& e) {
      std::cout << e.what() << '\n';
   }
}

輸出應如下所示:

std::bad_cast
typeinfo.htm
廣告

© . All rights reserved.