I agree, this tends to be confusing, but there are a lot of news in C++ (pun intended):
- One of them is the "
new
operator"
- Another one is "operator
new
" a function doing the actual memory allocation
- There is placement
new
- and some might not like the name, but there is also array
new
s (new[]
)
Not considering 3, 4 because they are worth a post of their own (What uses are there for "placement new"? and How do I use arrays in C++?), the first one ("new
operator") resides in the global namespace, and it is accessible with the the scope-resolution operator (::
), and the second one is the one you can overload in your classes. However regarding the relation of these two, there is an excellent explanation in MSDN (https://msdn.microsoft.com/en-us/library/kewsb8ba.aspx) I just paste it here:
The new operator invokes the function operator new. For arrays of any type, and for objects that are not of class, struct, or union types, a global function, ::operator new, is called to allocate storage. Class-type objects can define their own operator new static member function on a per-class basis.
When the compiler encounters the new operator to allocate an object of type type, it issues a call to type::operator new( sizeof( type ) ) or, if no user-defined operator new is defined, ::operator new( sizeof( type ) ). Therefore, the new operator can allocate the correct amount of memory for the object.