Cpp-standard-library-cpp-bad-cast
提供:Dev Guides
C ++ Type_infoライブラリ-bad_cast
説明
動的キャストに失敗すると例外をスローします。
宣言
- 以下は、std
- bad_castの宣言です。
C 98
class bad_cast;
C 11
class bad_cast;
パラメーター
none
戻り値
none
例外
スロー不可の保証-このメンバー関数は例外をスローしません。
データの競合
ロケールオブジェクトが変更されます。
例
- 以下の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