fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Base
  5. {
  6. public:
  7. virtual ~Base() {}
  8. };
  9.  
  10. class Derived: public Base
  11. {
  12. int i;
  13. public:
  14. void funOne()
  15. {
  16. cout << "inside funOne()" << endl ;
  17. }
  18. void funTwo()
  19. {
  20. cout << "inside funTwo()" << i << endl ;
  21. }
  22. };
  23.  
  24. int main()
  25. {
  26. Base* bptr = new Base;
  27. Derived* dptr = dynamic_cast<Derived*>(bptr);
  28.  
  29. cout << dptr << endl;
  30. cout << bptr << endl;
  31.  
  32. dptr->funOne(); // why this works?
  33. // dptr->funTwo(); // -> why this does NOT work? it throws exception
  34.  
  35. int temp;
  36. cin >> temp;
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
0
0x876c20
inside funOne()