You do not need a strict new one.
New ones may be prohibited from forcibly allocating a stack of objects (as opposed to heap allocation). Consider:
struct Point { int x, y; }; Point * a = new Point{3, 5};
So, if you delete the new operator (in C ++ 11 this is the right way if you want to disable it) or make it private (pre-C ++ 11), you use the protocol for users.
Another reason to ban (but not delete) newbies and constructors is to return some kind of smart pointer to your factory object:
class MyClass { private: MyClass(); void * operator new(...); public: static std::shared_ptr<MyClass> create() {
This will provide something in the factory constructor, if necessary, for example, using caches or any other thing, prohibit external users from using the new one as well, but still allow the use of the new inside the class to highlight (if you delete the new operator you can no longer do this do).
source share