C ++:析构函数和删除 [英] C++: destructor and delete

查看:81
本文介绍了C ++:析构函数和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




语句1:动态创建的本地对象在超出范围时会调用它的析构函数

方法一个程序返回


同意。

语句2:一个动态创建的对象将它称为析构函数

成为删除的目标。


对我没有意义。这不会导致无限循环吗? (即。

删除调用析构函数,在析构函数中调用delete,反过来

再次调用析构函数等等......)


任何评论都赞赏。


asasas

解决方案

在哪里这些语句来自?

你必须区分在堆上动态创建的对象与''new''运算符以及在作用域中的堆栈上创建的对象。

使用delete运算符指示程序调用类的析构函数
然后释放对象的内存。只是简单地超出范围的对象隐含地执行相同的过程。

Newsnet Customer < NI ******** @ iprimus.com.au>在消息中写道

新闻:3f ******** @ news.iprimus.com.au ...


声明1:当一个程序返回

同意时,一个动态创建的本地对象会在它超出范围时调用它的析构函数

语句2:动态创建的对象将在
将其作为删除目标时调用它的析构函数。

对我来说没有意义。这不会导致无限循环吗? (即。
删除调用析构函数,并在析构函数中调用delete,在
中再次调用析构函数等等......)

任何评论都赞赏。 />
asasas





" Newsnet Customer" < NI ******** @ iprimus.com.au>在消息中写道

新闻:3f ******** @ news.iprimus.com.au ...


声明1:当一个程序返回

同意时,一个动态创建的本地对象会在它超出范围时调用它的析构函数

语句2:动态创建的对象将在
将其作为删除目标时调用它的析构函数。

对我来说没有意义。这不会导致无限循环吗? (即。
删除调用析构函数,并在析构函数中调用delete,在
中再次调用析构函数等等......)

任何评论都赞赏。 asasas




析构函数不会调用delete,因此没有循环。


john




" Newsnet Customer" < NI ******** @ iprimus.com.au>在消息中写道

新闻:3f ******** @ news.iprimus.com.au ...


声明1:当一个程序返回

同意时,动态创建的本地对象将在它超出范围时调用它的析构函数
方法。


考虑这段代码 -


#include< iostream>

A级{

public:

A()

{

}

~A()

{

std :: cout<< Inside D''tor" ;;

}

};


void f()

{

A a;

A * ptrA =新A;

}


int main()

{

f();

}


如果你的意思是f()返回两个析构函数将被调用然后它'

错误。

析构函数只会被调用在堆上创建的对象。

实际上这是一个内存泄漏。


语句2:动态创建的对象将调用它的析构函数,当
它成为目标删除。


这是真的。


对我来说没有意义。这不会导致无限循环吗? (即。
删除调用析构函数,并在析构函数中调用delete,在
中再次调用析构函数,依此类推......)



析构函数除非你的析构函数是你写的,否则不要调用删除 -

删除这个;


HTH,

J.Schafer


Hi,

Statement 1: "A dynamically created local object will call it''s destructor
method when it goes out of scope when a procedure returms"

Agree.
Statement 2: "A dynamically created object will call it''s destructor when it
is made a target of a delete".

Does not make sense to me. Would not this result in an endless loop? (ie.
delete calls destructor and within destructor it calls delete, which in turn
calls the destructor again and so on....)

Any comments appreciated.

asasas

解决方案

Where are these statements from?
You have to differentiate between objects created dynamically on the heap
with the ''new'' operator, and objects created on the stack in a scope.
Using the delete operator instructs the program to call the destructor of
the class and then free the memory for the object. Objects that simply go
out of scope implicitely have the same process performed on them.
"Newsnet Customer" <ni********@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...

Hi,

Statement 1: "A dynamically created local object will call it''s destructor
method when it goes out of scope when a procedure returms"

Agree.
Statement 2: "A dynamically created object will call it''s destructor when it is made a target of a delete".

Does not make sense to me. Would not this result in an endless loop? (ie.
delete calls destructor and within destructor it calls delete, which in turn calls the destructor again and so on....)

Any comments appreciated.

asasas




"Newsnet Customer" <ni********@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...

Hi,

Statement 1: "A dynamically created local object will call it''s destructor
method when it goes out of scope when a procedure returms"

Agree.
Statement 2: "A dynamically created object will call it''s destructor when it is made a target of a delete".

Does not make sense to me. Would not this result in an endless loop? (ie.
delete calls destructor and within destructor it calls delete, which in turn calls the destructor again and so on....)

Any comments appreciated.

asasas



Destructor does not call delete, so there is no loop.

john



"Newsnet Customer" <ni********@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...

Hi,

Statement 1: "A dynamically created local object will call it''s destructor
method when it goes out of scope when a procedure returms"

Agree.
Consider this code -

#include <iostream>
class A{
public:
A()
{
}
~A()
{
std::cout << "Inside D''tor";
}
};

void f()
{
A a;
A *ptrA = new A;
}

int main ()
{
f ();
}

If you mean that when f() returns two destructors will be called then it''s
wrong.
Destructor would be called only for the object created on the heap.
In fact this is a memory leak.

Statement 2: "A dynamically created object will call it''s destructor when it is made a target of a delete".

That''s true.

Does not make sense to me. Would not this result in an endless loop? (ie.
delete calls destructor and within destructor it calls delete, which in turn calls the destructor again and so on....)


Destructor does not call delete unless in your destructor you write -
delete this;

HTH,
J.Schafer


这篇关于C ++:析构函数和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆