fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define HERAKLIT_TASK 0
  5.  
  6. class HeapOnly {
  7. public:
  8. HeapOnly() {}
  9. void destroy() const { delete this; }
  10. protected:
  11. ~HeapOnly() {}
  12. };
  13. //HeapOnly h1; // Destructor is protected so h1 can't be created globally
  14. //HeapOnly func() // Compiler error because destructor of temporary is protected
  15. //{
  16. // HeapOnly *hoptr = new HeapOnly; // This is ok. No destructor is invoked automatically for heap-based objects
  17. // return *hoptr;
  18. //}
  19. int main(void) {
  20. #if HERAKLIT_TASK
  21. HeapOnly h2; // Destructor is protected so h2 can't be created on stack
  22. #else
  23. HeapOnly * hptr = new HeapOnly();
  24. hptr->destroy();
  25. #endif
  26. return 0;
  27. }
Success #stdin #stdout 0s 3452KB
stdin
Standard input is empty
stdout
Standard output is empty