I found this code sample for 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 I look at the documentation for new
there is no version that takes a pointer. Thus:
It is called "placement new", and the comments in your code snippet pretty much explain it:
It constructs an object of type T
without allocating memory for it, in the address specified in the parentheses.
So what you're looking at is a copy assignment operator which first destroys the object being copied to (without freeing the memory), and then constructs a new one in the same memory address. (It is also a pretty bad idea to implement the operator in this manner, as pointed out in the comments)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With