fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. class Base
  7. {
  8. public:
  9. Base() { ptr = new int[100]; cout << "alloc mem at " << ptr << endl; }
  10. ~Base() { delete [] ptr; cout << "free mem at " << ptr << endl; }
  11. int * ptr;
  12. };
  13.  
  14. class Derived1
  15. {
  16. public:
  17. Derived1(int x, int y):x(x),y(y){}
  18. Derived1(int x)
  19. {
  20. new(this) Derived1(x,0);
  21. }
  22. int x, y;
  23. Base b;
  24. };
  25.  
  26. class Derived2: public Base
  27. {
  28. public:
  29. Derived2(int x, int y):x(x),y(y){}
  30. Derived2(int x)
  31. {
  32. new(this) Derived2(x,0);
  33. }
  34. int x, y;
  35. };
  36.  
  37.  
  38. int main(int argc, const char * argv[])
  39. {
  40. Derived1 d1(5);
  41. Derived2 d2(5);
  42. }
  43.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
alloc mem at 0x9f6fa10
alloc mem at 0x9f6fba8
alloc mem at 0x9f6fd40
alloc mem at 0x9f6fed8
free mem at 0x9f6fed8
free mem at 0x9f6fba8