Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
bad_cast
#include <iostream> #include <type_traits> #include <functional> struct B { int b; virtual int bb(){return 1;};}; struct C { int c; virtual int cc(){return 2;};}; struct D { int d; virtual void dd(){}; ~D(){} }; class A : public B, public C, public D { int a; }; int main() { A a1; D* d1 = &a1; D d2; D& d2r = d2; std::cout << "a1: " << &a1 << std::endl; std::cout << "d1: " << d1 << std::endl; std::cout << "d2: " << &d2 << std::endl; std::cout << "static_cast<A*>(d1): " << static_cast<A*>(d1) << std::endl; std::cout << "static_cast<A&>(d2): " << &(static_cast<A&>(d2)) << std::endl; // does not throw try { std::cout << "dynamic_cast<A&>(d2): " << &(dynamic_cast<A&>(d2r)) << std::endl; // throws } catch (std::bad_cast& e) { std::cout << "bad_cast" << std::endl; } std::cout << dynamic_cast<A*>(d1) << std::endl; std::cout << ((void*)dynamic_cast<A*>(d1) == (void*)d1) << std::endl; }
run
|
edit
|
history
|
help
0
лаб1
ambiguity does not count as ambiguity
Problem_onoff_3
xxx
Struct packing
dynamic_cast in assert Causing Error
javascritp
virtual inheritance
Generic EventProvider
codecvt wide string conversion with multibyte chars and locale + concatenation