fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define HERAKLIT_TASK 1
  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. delete hptr;
  25. #endif
  26. return 0;
  27. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:11:5: error: 'HeapOnly::~HeapOnly()' is protected
     ~HeapOnly() {}
     ^
prog.cpp:21:12: error: within this context
   HeapOnly h2; // Destructor is protected so h2 can't be created on stack
            ^
stdout
Standard output is empty