C++ Exception Library - bad_cast
Description
This is an exception thrown on failure to dynamic cast.
Declaration
Following is the declaration for std::bad_cast.
class bad_cast;
C++11
class bad_cast;
Parameters
none
Return Value
none
Exceptions
No-throw guarantee − no members throw exceptions.
Example
In below example for 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';
}
}
The sample output should be like this −
std::bad_cast
exception.htm
Advertisements