fork download
  1. #include <iostream>
  2. using namespace std;
  3. class base
  4. {
  5. public:
  6. int i = 99;
  7. virtual ~base(){}
  8. };
  9. class derv: public base
  10. {
  11. public:
  12. void f() { cout << "derv is called" << i << endl;}
  13. };
  14. int main() {
  15. base* p = new base();
  16. derv *d1 = dynamic_cast<derv*>(p);
  17. if(d1 == nullptr)
  18. {
  19. std::cout << "nullptr\n";
  20. }
  21.  
  22. // Since p point to base , so d1 should return nullptr
  23. //calling any function using d1, should fail/crash
  24. //but why the following line is working ??
  25.  
  26. d1->f();
  27. }
Runtime error #stdin #stdout 0s 4432KB
stdin
Standard input is empty
stdout
Standard output is empty