Item 35. Placement New
It’s impossible to call a constructor directly. However, we can trick the compiler into calling a constructor for us through the use of placement new.
void *operator new( size_t, void *p ) throw() { return p; }
Placement new is a standard, global, overloaded version of operator new that cannot be replaced with a user-defined version (unlike the standard, global, “usual” operator new and operator delete that can be replaced but probably shouldn’t be). The implementation ignores the size argument and returns its second argument. This has the effect of allowing one to “place” an object at a particular location, giving the effect of being able to call a constructor.
It’s important to distinguish the new operator from ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access