fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;
  4.  
  5. class X { virtual void whatever(){}};
  6. class Y : public X {public: int a;};
  7.  
  8. int main() {
  9. try{
  10. X* x = new X;
  11. X* y = new Y;
  12. Y &yy = dynamic_cast<Y&>(*x);
  13. cout << yy.a;
  14. } catch(std::exception& e) {cout << e.what() << endl;}
  15. }
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:11: warning: unused variable ‘y’
stdout
std::bad_cast