fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class IObject
  5. {
  6. public:
  7. virtual int getType()=0;
  8. };
  9. class Base : public IObject
  10. {
  11. protected:
  12. int val;
  13. public:
  14. Base() { val = 1; }
  15. virtual int getType();
  16. };
  17. int Base::getType() { return val; }
  18.  
  19. class Derived : public virtual Base //If I remove the virtual keyword here problem is solved.
  20. {
  21. public:
  22. Derived() { val = 2; }
  23. };
  24.  
  25. int getVal( void* ptr )
  26. {
  27. return ((IObject*)ptr)->getType();
  28. }
  29.  
  30. int main()
  31. {
  32. IObject* ptr = new Derived();
  33. cout << getVal(ptr) << endl;
  34. return 0;
  35. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
2