I haven't programmed much since before the C++11 standard, so I'm still learning some of the newer idioms and how to use them.
I've been thinking how to write efficient assignment operators, and I just found out how placement new works.
So now I'm considering writing assignment operators by (1) destroying the object, and (2) using placement new to call the copy constructor, like:
MyClass& MyClass::operator=(const MyClass& other)
{
if (&other == this)
return (*this);
this -> ~MyClass();
return *(new (this) MyClass(other));
}
Normally I would never use an object after destructing it. BUT, I am immediately reconstructing it. Is this idiom safe and elegant to use? Or is it a TERRIBLE idea and I should immediately
delete this;