◐ Shell
clean mode source ↗

std::bad_cast - cppreference.com

De cppreference.com

<metanoindex/>

<tbody> </tbody>

Definido no cabeçalho

<typeinfo>

class bad_cast : public std::exception;

Uma exceção deste tipo é acionada quando um

dynamic_cast

a um tipo de referência falhar a verificação em tempo de execução (por exemplo, porque os tipos não estão relacionadas por herança).

Original:

An exception of this type is thrown when a

dynamic_cast

to a reference type fails the run-time check (e.g. because the types are not related by inheritance).

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

cpp/error/exception

Inheritance diagram

Funções de membro

constrói um objeto bad_cast novo

Original:

constructs a new bad_cast object

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(função pública membro)

Herdado de std::exception

Member functions

destrói o objeto de exceção

Original:

destructs the exception object

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(of std::exception função pública virtual membro) [edit]

retorna uma cadeia explicativa

Original:

returns an explanatory string

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(of std::exception função pública virtual membro) [edit]

Exemplo

#include <iostream>
#include <typeinfo>

struct Foo { virtual void f() {} };
struct Bar { virtual void f() {} };

int main()
{
    Bar b;
    try {
        Foo& f = dynamic_cast<Foo&>(b);
    } catch(const std::bad_cast& e)
    {
        std::cout << e.what() << '\n';
    }
}

Saída: