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