std::bad_cast
cppreference.com
<tbody>
</tbody>
class bad_cast : public std::exception; |
||
, dynamic_cast (, ), - std::use_facet, .
-
() |
bad_cast (public -) |
operator= |
bad_cast (public -) |
what |
(public -) |
std::bad_cast::bad_cast
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>| (1) | ||
bad_cast() throw(); |
( C++11) | |
bad_cast() noexcept; |
( C++11) | |
| (2) | ||
bad_cast( const bad_cast& other ) throw(); |
( C++11) | |
bad_cast( const bad_cast& other ) noexcept; |
( C++11) | |
bad_cast , , what().
1) .
2) .
*this other std::bad_cast , std::strcmp(what(), other.what()) == 0. ( C++11)| other |
std::bad_cast::operator=
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody> bad_cast& operator=( const bad_cast& other ) throw(); |
( C++11) | |
bad_cast& operator=( const bad_cast& other ) noexcept; |
( C++11) | |
other. *this other std::bad_cast, std::strcmp(what(), other.what()) == 0 . ( C++11)
| other |
*this
std::bad_cast::what
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody> virtual const char* what() const throw(); |
( C++11) | |
virtual const char* what() const noexcept; |
( C++11) | |
.
()
. std::wstring. , , , , , - (, ) .
, what().
std::exception
-
[virtual] |
(virtual public of std::exception -)
|
[virtual] |
(virtual public of std::exception -)
|
#include <iostream>
#include <typeinfo>
struct Foo { virtual ~Foo() {} };
struct Bar { virtual ~Bar() { std::cout << "~Bar\n"; } };
struct Pub : Bar { ~Pub() override { std::cout << "~Pub\n"; } };
int main()
{
Pub pub;
try
{
[[maybe_unused]]
Bar& r1 = dynamic_cast<Bar&>(pub); // OK,
[[maybe_unused]]
Foo& r2 = dynamic_cast<Foo&>(pub); //
}
catch(const std::bad_cast& e)
{
std::cout << "e.what(): " << e.what() << '\n';
}
}
:
e.what(): std::bad_cast
~Pub
~Bar