c++ - What does new (this) mean? -
i found code sample study:
t & t::operator=(t const & x) { if (this != &x) { this->~t(); // destroy in place new (this) t(x); // construct in place } return *this; }
when @ the documentation new
there no version takes pointer. thus:
- what new (this) mean?
- what used for?
- how can called if not listed in documentation?
it called "placement new", , comments in code snippet pretty explain it:
it constructs object of type t
without allocating memory it, in address specified in parentheses.
so you're looking @ copy assignment operator first destroys object being copied (without freeing memory), , constructs new 1 in same memory address. (it pretty bad idea implement operator in manner, pointed out in comments)
Comments
Post a Comment