c++ - Peculiar way for object creation? -
here example of implementing class creating objects:
template <class t> struct malloccreator { static t* create() { void* buf = std::malloc(sizeof(t)); if (!buf) return 0; return new(buf) t; } };
by using malloc
, new
operator.
what role of new
, above syntax?
it's "placement new". code create raw memory size of 1 t
malloc
, construct new t
in raw memory.
Comments
Post a Comment