◐ Shell
clean mode source ↗

std::bad_cast - cppreference.com

提供: cppreference.com

<tbody> </tbody>

class bad_cast : public std::exception;

参照型の dynamic_cast で実行時型チェックに失敗 (指定された型が継承関係にないなど) したとき、この型の例外が投げられます。 また、要求されたファセットがロケールに存在しない場合も、 std::use_facet によって投げられます。

cpp/error/exception

継承図

メンバ関数

新しい 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';
    }
}

出力例: