fork download
  1. #include <new>
  2. #include <cstdlib>
  3.  
  4. class X
  5. {
  6. protected:
  7. static void* operator new(size_t sz);
  8. static void operator delete(void* p) noexcept;
  9. };
  10.  
  11. class Y : public X { }; // inheritance is ok
  12. class Z { X x; }; // ok
  13.  
  14. X x1; // ok
  15. int main()
  16. {
  17. X x2; // ok
  18. Y y1; // ok
  19. X* xp = new X; // syntax error
  20. Y* yp = new Y; // syntax error
  21.  
  22. delete xp; // syntax error
  23. delete yp; // syntax error
  24. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:19:15: error: ‘static void* X::operator new(size_t)’ is protected within this context
   X* xp = new X;         // syntax error
               ^
prog.cpp:7:16: note: declared protected here
   static void* operator new(size_t sz);
                ^~~~~~~~
prog.cpp:20:15: error: ‘static void* X::operator new(size_t)’ is protected within this context
   Y* yp = new Y;         // syntax error
               ^
prog.cpp:7:16: note: declared protected here
   static void* operator new(size_t sz);
                ^~~~~~~~
prog.cpp:22:10: error: ‘static void X::operator delete(void*)’ is protected within this context
   delete xp;             // syntax error
          ^~
prog.cpp:8:16: note: declared protected here
   static void  operator delete(void* p) noexcept;
                ^~~~~~~~
prog.cpp:22:10: error: ‘static void X::operator delete(void*)’ is protected within this context
   delete xp;             // syntax error
          ^~
prog.cpp:8:16: note: declared protected here
   static void  operator delete(void* p) noexcept;
                ^~~~~~~~
prog.cpp:23:10: error: ‘static void X::operator delete(void*)’ is protected within this context
   delete yp;             // syntax error
          ^~
prog.cpp:8:16: note: declared protected here
   static void  operator delete(void* p) noexcept;
                ^~~~~~~~
prog.cpp:23:10: error: ‘static void X::operator delete(void*)’ is protected within this context
   delete yp;             // syntax error
          ^~
prog.cpp:8:16: note: declared protected here
   static void  operator delete(void* p) noexcept;
                ^~~~~~~~
stdout
Standard output is empty