fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. struct Test
  8. {
  9. Test() { cout << "Выделяем кучу памяти\n"; }
  10. ~Test() { cout << "Освобождаем кучу памяти\n"; }
  11. virtual void foo()
  12. {
  13. new ( this ) Test;
  14. }
  15.  
  16. virtual void bar()
  17. {
  18. new ( this ) Test;
  19. foo();
  20. }
  21. };
  22.  
  23. int main()
  24. {
  25. Test test;
  26. test.bar();
  27. return 0;
  28. }
  29.  
  30.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Выделяем кучу памяти
Выделяем кучу памяти
Выделяем кучу памяти
Освобождаем кучу памяти