I know new
and delete
are keywords.
int obj = new int;
delete obj;
int* arr = new int[1024];
delete[] arr;
<new>
header is a part of C++ standard headers. It has two operators (I am not sure they are operators or they are functions):
::operator new
::operator delete
these operators used like below:
#include <new>
using namespace std;
int* buff = (int*)::operator new(1024 * sizeof(int));
::operator delete(buff);
What are "::operator new" and "::operator delete"? Are they different from new
and delete
keywords?
::
tells the compiler to call the operators defined in global namespace.
It is the fully qualified name for the global new
and delete
operators.
Note that one can replace the global new
and delete
operators as well as overload class-specific new
and delete
operators. So there can be two versions of new
and delete
operators in an program. The fully qualified name with the scope resolution operator tells the compiler you are referring to the global version of the operators and not the class-specific ones.