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 ...
Get C++ Common Knowledge: Essential Intermediate Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.