许多类的赋值运算符(operator=)与析构函数中的代码相同,并且与复制构造函数的代码非常相似。
那么以这种方式实施分配是个好主意吗?
Point& operator=(const Point& point)
{
if(&point != this)
{
//Call the destructor
this->~Point();
//Make the placement new
//Assignment is made because some compilers optimise such code as just
// new Point;
Point* p_n = new (this) Point(point);
//We where placing in this place so pointers should be equal
assert(p_n == this);
}
return *this;
}