Mat*_*att 0 c++ destructor assignment-operator
例如:
class Foo : public Bar
{
~Foo()
{
// Do some complicated stuff
}
Foo &operator=(const Foo &rhs)
{
if (&rhs != this)
{
~Foo(); // Is this safe?
// Do more stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
在继承和其他类似的事情上明确调用析构函数会产生什么意外后果吗?
有没有理由将析构函数代码抽象为void destruct()函数并调用它?
在最简单的情况下调用析构函数是一个坏主意,而在代码变得稍微复杂的那一刻,调用析构函数是一个可怕的.
最简单的情况是:
class Something {
public:
Something(const Something& o);
~Something();
Something& operator =(const Something& o) {
if (this != &o) {
// this looks like a simple way of implementing assignment
this->~Something();
new (this) Something(o); // invoke copy constructor
}
return *this;
}
};
Run Code Online (Sandbox Code Playgroud)
这是一个坏主意.如果复制构造函数抛出,则会留下原始内存 - 那里没有对象.只有在赋值运算符之外,没有人注意到.
如果继承发挥作用,情况会变得更糟.假设Something实际上是一个带有虚析构函数的基类.派生类的函数都使用默认值实现.在这种情况下,派生类的赋值运算符将执行以下操作:
在这一点上,你有多个UB的实例相互堆叠,在一堆完整的混乱中.
是的.不要那样做.