std::bad_cast
提供: cppreference.com
<tbody>
</tbody>

| ヘッダ <typeinfo> で定義
|
||
class bad_cast : public std::exception; |
||
参照型の dynamic_cast で実行時型チェックに失敗 (指定された型が継承関係にないなど) したとき、この型の例外が投げられます。 また、要求されたファセットがロケールに存在しない場合も、 std::use_facet によって投げられます。
継承図
メンバ関数
| 新しい bad_cast オブジェクトを構築します (パブリックメンバ関数) |
std::exception から継承
メンバ関数
[仮想] |
例外オブジェクトを破棄します ( std::exceptionの仮想パブリックメンバ関数)
|
[仮想] |
説明文字列を返します ( std::exceptionの仮想パブリックメンバ関数)
|
例
Run this code
#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';
}
}
出力例:
Bad dynamic cast