C ++中的删除和调用析构函数之间有什么区别 [英] What is the difference between delete and calling destructor in C++
本文介绍了C ++中的删除和调用析构函数之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如标题中所述,这是我的代码:
As stated in the title, here is my code:
class Foo {
public:
Foo (int charSize) {
str = new char[charSize];
}
~Foo () {
delete[] str;
}
private:
char * str;
};
对于本课程,这之间的区别是:
For this class what would be the difference between:
int main () {
Foo* foo = new Foo(10);
delete foo;
return 0;
}
和
int main () {
Foo* foo = new Foo(10);
foo->~Foo();
return 0;
}
推荐答案
调用析构函数会释放资源由对象拥有,但不会释放分配给对象本身的内存。第二个代码段内存泄漏。
Calling a destructor releases the resources owned by the object, but it does not release the memory allocated to the object itself. The second code snippet has a memory leak.
这篇关于C ++中的删除和调用析构函数之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文