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 ?
Asked
Active
Viewed 472 times
3
-
Do you know what it does in other contexts? (If not, why not?) Do you have an expectation that it should mean something else for `new`? (If so, why?) <--- answering these will make your question great again – Lightness Races in Orbit May 25 '17 at 11:10
1 Answers
4
::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

John Zwinck
- 239,568
- 38
- 324
- 436
-
Placement new deserves a point of mention; that is my most common use of `::new`. – Yakk - Adam Nevraumont May 25 '17 at 11:30