People sometimes seem to like to deliberately make C + + language terminology difficult to understand. For example, the difference between the new operator (new operator) and operator new.
When you write this code:
string *ps = new string("Memory Management");
The new you are using is the new operator. This operator, like sizeof, is built into the language, and you can't change its meaning, it's always the same function. The functionality it has to complete is divided into two parts. The first part is the object that allocates enough memory to hold the desired type. The second part is that it calls the constructor to initialize the object in memory. The new operator always does both things, and you can't change its behavior in any way.
What you can change is how to allocate memory for an object. The new operator calls a function to complete the necessary memory allocation, and you can override or overload the function to change its behavior. The new operator is the name of the function called operator new for allocating memory.
function operator new usually declares this way:
void * operator new(size_t size);
The return value type is void*, because this function returns an unprocessed (raw) pointer to the uninitialized memory. (If you like, you can write a operator new function that can initialize memory to store values before returning a pointer, but generally not.) Parameter size_t determine how much memory is allocated. You can add additional parameters to the overloaded function operator new, but the first parameter type must be size_t. (For more information about operator new See effective C + + clause 8 to clause 10.) )
You don't normally call operator new, but once you do, you can call it just like you call other functions:
void *rawMemory = operator new(sizeof(string));
Operator operator new returns a pointer to a piece of memory that is sufficient to hold a string object.
Just like malloc, operator New's job is to allocate memory. It knows nothing about constructors. Operator new is about memory allocation. Passing the unhandled pointer returned by operator new to an object is the work of the new operator. When your compiler encounters such a statement: