in an example i saw this line
Thing *pThing = new (getHeap(), getConstraint()) Thing(initval());
There was no explanation, function body or class definition. What does the 2nd parameter mean?
-
It's an instance of 'placement
new' syntax. It's for passing additional parameters to a custom memory allocation function.Whereas this:
Obj* pObj = new Obj;corresponds to allocating new memory by calling
operator newwith a single parameter of typesize_tand the value ofsizeof(Obj), and constructing a new Obj instance in the returned memory location,Obj* pObj = new (param1, param2) Obj;corresponds to calling an
operator newwith three parameters,sizeof(Obj)followed byparam1andparam2and constructing the Obj instance in the memory pointed to by the return value of the customoperator new.Custom
operator news can be defined globally, or as implicitly static class members, in which case they will only be considered for allocating class instances of that type or derived types.There's more hows and whys in this FAQ.
Arafangion : A flawless answer - nothing can be added or removed.acidzombie24 : This answer is pure gold
0 comments:
Post a Comment