I'm using OpenSplice DDS, and there, almost all C++ classes (basic ones that I used, I can mention them if that matters) have overloaded new
operators to be private (to prevent users from using them). I don't understand, why would anyone do that? Could someone provide some examples that show the necessity of this?
Why I need new
: Because most of these classes don't have default constructors, and I need to initialize them later in my implementation through a unique_ptr
.
Easy trick: On the other hand... I can very easily trick this! I can just wrap this class with another class, and use new
all I want, right? Hence I don't understand the motivation and it feels like bad style. Could someone explain?
EDIT:
Just to clarify: Providing a good example where this can't be escaped is a good answer. It'll be helpful for all people who see new
operators made private.