fork download
  1. #include <iostream>
  2.  
  3. struct Base
  4. {
  5. virtual ~Base(){}
  6. virtual void Print() const
  7. {
  8. std::cout << "Base" << std::endl;
  9. }
  10. };
  11.  
  12. struct Derived : Base
  13. {
  14. virtual ~Derived(){}
  15. virtual void Print() const
  16. {
  17. std::cout << "Derived" << std::endl;
  18. }
  19. };
  20.  
  21. int main()
  22. {
  23. Base *r1 = new Base;
  24. Base *r2 = new Derived;
  25.  
  26. r1->Print();
  27. r2->Print();
  28.  
  29. r1->~Base();
  30. r2->~Base();
  31. new ((void*)r1) Derived;
  32. new ((void*)r2) Base;
  33.  
  34. r1->Print();
  35. r2->Print();
  36.  
  37. delete r1;
  38. delete r2;
  39. }
  40.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
Base
Derived
Derived
Base