清理后删除它
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问题。与其问我们你对析构函数已经做的奇怪的重新实现,不如问问你在析构函数中调用虚函数时遇到的问题。
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 将指针设置为"nullptr"并不能防止双重删除?
- 为什么示例代码访问IUnknown中已删除的内存
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- C++中的线程安全删除
- 如何从存储在std::映射中的std::集中删除元素
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数