Possibly you are conflating a new-expression with operator new
.
An operator new
is an allocation function, with a confusing name. It should better have been called _alloc
or some such.
A new-expression
-
calls the allocation function, passing any specified arguments.
-
if that succeeds, calls the class constuctor with specified arguments.
Here the relevant constructor must be accessible. -
if that fails, cleans up by deallocating memory (in the case where the allocation function has user defined arguments, a corresponding user defined deallocation function is called with the originally specified custom allocation function arguments).
In short, a new-expression guarantees that if you have fulfilled your obligations, then you will either have an initialized object at hand, or an exception with no memory leak.
One way to not fulfill your obligations is to defined a custom allocation function with no custom deallocation function. Then in the last bullet point above you get no cleanup, and hence a memory leak. But other than that, a new-expression is almost like a database transaction, it guarantees all-or-nothing.
3
solved Not able to create the object by new if new operator is overloaded and constructor is private