c++ - 由于 delete 运算符释放内存,为什么我需要一个析构函数?

标签 c++ destructor delete-operator

来自 C++ 常见问题解答:http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9

Remember: delete p does two things: it calls the destructor and it deallocates the memory.

如果 delete 释放内存,那么这里的析构函数有什么用?

最佳答案

If delete deallocates the memory, then what's the need of the destructor here?

析构函数的要点是执行清理对象后所需的任何逻辑,例如:

  • 对被销毁对象拥有的其他对象调用 delete
  • 适当释放其他资源,如数据库连接;文件句柄等

关于c++ - 由于 delete 运算符释放内存,为什么我需要一个析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9125271/

相关文章:

c++ - 在 C++ 中删除时间数组

c++ - 如何删除空指针?

c++ - C++中删除运算符的困惑

c++ - 动态内存分配

c++ - 如何以可维护的方式为整个类层次结构保护析构函数?

c++ - 来自二维数组的特征图

c++ - 为什么 std::unary_function 不包含虚拟析构函数

c++ - 编译器真的强制执行纯虚拟析构函数吗?

c++ - 在 Windows CryptoAPI 和 OpenSSL x509 格式之间转换

c++ - 模板类和 typedef