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