fork download
  1. #include <iostream>
  2.  
  3. struct pad {
  4. int x;
  5.  
  6. virtual ~pad() {}
  7. };
  8.  
  9. struct b {
  10. int x;
  11. };
  12.  
  13. struct vb {
  14. int x;
  15.  
  16. virtual ~vb() {}
  17. };
  18.  
  19. struct d : pad, b, vb {};
  20.  
  21. void operator delete( void *p ) {
  22. std::cout << "free " << p << '\n';
  23. }
  24.  
  25. int main() {
  26. std::cout << "With virtual destructor:\n";
  27. d *p = new d;
  28. std::cout << "allocate " << p << ", delete " << static_cast< vb * >( p ) << '\n';
  29. delete static_cast< vb * >( p );
  30.  
  31. std::cout << "With plain destructor:\n";
  32. p = new d;
  33. std::cout << "allocate " << p << ", delete " << static_cast< b * >( p ) << '\n';
  34. delete static_cast< b * >( p );
  35. }
  36.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
With virtual destructor:
allocate 0x8739008, delete 0x8739014
free 0x8739008
With plain destructor:
allocate 0x8739020, delete 0x8739028
free 0x8739028