Search code examples
c++c++11scope-resolution

when do we use scope resolution operator before new ( ::new)?


I came across a code where scope resolution operator is placed before new. when do we use it. what is the meaning of it. Please anybody can explain ?


Solution

  • ::new is the explicit global operator. This is as opposed to the various class-scoped operators new which may be defined. For example, if I define an operator new inside myclass, and then in that same class I want to use the global one, I would say ::new, whereas if I said new I would get the class-specific function I defined.

    I might also use ::new in generic template code where I am not sure what type I might be allocating, but want to make sure I do not use any class-specific allocator (for example I might need to pass the result to some API which will use global ::delete on it).

    Here's a big list of all possible operators new for reference: http://en.cppreference.com/w/cpp/memory/new/operator_new