清理后删除它

Delete this after cleanup

本文关键字:删除      更新时间:2023-10-16
class BaseTest {
protected:
  virtual void finalizeParts() {
      delete m_part;
  };
public:
  int* m_part;
  BaseTest() {
    m_part = new int;
  }
  void finalize() {
    finalizeParts();
    delete this;
  }
};
class SubTest : public BaseTest {
protected:
  void finalizeParts() {
      BaseTest::finalizeParts();
      delete m_anotherPart;
  }
public:
  int* m_anotherPart;
  SubTest() {
    m_anotherPart = new int;
  }
};
SubTest* test = new SubTest();
test->finalize();

我在类的析构函数中避免虚拟函数调用时遇到麻烦(我讨厌C++首先迫使我这样做(。与基类中使用一些可以在需要时重写的虚拟函数的常见清理策略相比,覆盖我所有子类中的析构函数似乎非常不方便。作为一种解决方法,我正在考虑在我的基类中引入一个"finalize"函数来执行清理并最终调用"删除这个"。这可能是一个非常不寻常的解决方案,所以我想知道我是否以错误的方式接近它,以及是否有一种更明显的方法来编写 C++ 中的"正确"析构函数。

如果您的对象聚合了管理其资源的其他对象(例如,想想智能指针。一般原理称为 RAII(,那么根本不需要析构函数。

当然,您仍然需要为每个这样的"管理器"配备 desructor ,但由于需要关注的信息和资源较少,因此应该更容易实现。

在您的情况下,自删除似乎是不好的方法,因为没有人会期望finalize释放对象的内存(最小惊讶原则(。尽管内部实现可能没问题(例如,我曾经delete this何时实现shared_ptr。也就是说,当资源管理器的计数器达到 0 时,我做到了。但无论如何,这种行为并没有暴露给最终用户(

你的方法错了。

使用虚拟析构函数,将调用基类的析构函数以及派生类的析构函数 - 因此您应该为每个派生类实现析构函数,这将仅删除派生类的相关部分,并让基类的析构函数清理基类。

如果你有一个派生类链,则将调用每个继承级别的析构函数 - 按从大多数派生类到基类的顺序。

对于您的示例,所需的只是:

class BaseTest {
public:
  virtual ~BaseTest () {
      delete m_part;
  };
  int* m_part;
  BaseTest() {
    m_part = new int;
  }
};
class SubTest : public BaseTest {
 public:
  virtual ~SubTest () {
      //~BaseTest() will be called automatically
      delete m_anotherPart;
  }
  int* m_anotherPart;
  SubTest() {
    m_anotherPart = new int;
  }
};
SubTest* test = new SubTest();
delete test;

这对你来说更简单吗?

class BaseTest {
public:
  std::unique_ptr<int> m_part;
  BaseTest() : m_part(new int) {}
  virtual ~BaseTest() {}
};
class SubTest : public BaseTest {
public:
  std::unique_ptr<int> m_anotherPart;
  SubTest() : m_anotherPart(new int) {}
};
SubTest test;

或者就此而言,

class BaseTest {
public:
  int m_part;
};
class SubTest : public BaseTest {
public:
  int m_anotherPart;
};
SubTest test;

我认为您有一个XY问题。与其问我们你对析构函数已经做的奇怪的重新实现,不如问问你在析构函数中调用虚函数时遇到的问题。