Why would you make the `new` operator private?

I use OpenSplice DDS , and there almost all C ++ classes (the base ones that I used, I can mention, if that matters) are overloaded with new for private users (so that users cannot use them). I don’t understand why anyone should do this? Can someone provide some examples that show the need for this?

Why I need new : since most of these classes do not have default constructors, and I need to initialize them later in my implementation with unique_ptr .

Easy trick: On the other hand ... I can very easily fool it! I can just wrap this class with another class and use new whatever I want, right? Therefore, I do not understand the motivation, and it seems to me that this is a bad style. Can someone explain?


EDIT:

Just to clarify: A good example of this being impossible to avoid is a good answer. This will be useful for all people who see new operators made private.

+6
source share
3 answers

A lot of built-in C ++ should ensure that dynamic allocations will not execute any dynamic allocations. This requirement requires aeronautics, automotive and medical equipment. See the following links for such coding standards and the rational behind it:

+3
source

why does someone [overload new operators privately (so that users cannot use them)]

Presumably to prevent the allocation of objects directly to the heap (i.e. dynamically).

Can someone provide some examples that show the need for this?

I do not know anything that requires this. The best source for finding information about such a solution is documentation. If it is not documented, you can ask the developers.

I can only guess. I assume that designers hope to make it more difficult for their users to make mistakes, forgetting about free dynamic objects or trying to destroy or use nonexistent ones.


EDIT: This is not officially official, but a user marked as an employee of a library company commented on his forum :

The ISO C ++ API is designed to create all objects on the stack, not on the heap, as this allows you to automatically manage memory depending on whether the objects are referenced by objects. Copying a C ++ ISO object will not lead to overhead, as it will simply copy the hidden smart pointer, which will lead to two references to the same object.


Since most of these classes do not have default constructors, and I need to initialize them later ...

This does not necessarily mean that you need dynamic allocation. Even if a class may not have a default constructor, it can still have a reasonable default state, which can be achieved by passing some canonical value to the constructor (for example, nullptr ).

+3
source

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}; //sizeof(Point) in heap + sizeof(Point *) in stack. Point b{3, 5}; //sizeof(Point), directly in the stack 

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() { //check if object in cache... auto c = std::make_shared<MyClass>(); //Do more stuff maybe return c; } }; 

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).

+2
source

Source: https://habr.com/ru/post/1014643/


All Articles