Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
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
Please
log in
to post a comment.
namespace name resolution
😊
C string literal
MSVC alias template
cross-platform sleep function
sharedptr emptiness
5sdgts
Sum of numbers in a series using sum formular
problem_binary_4
no-thread
stackse - search stackoverflow differently
Please log in to post a comment.