我可以删除而不调用析构函数吗? [英] Can I delete without calling the destructor ?

查看:72
本文介绍了我可以删除而不调用析构函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我有一个班级对象的链表。我认为当我删除第一个

元素时,
让析构函数删除整个列表会很好。我不想递归破坏列表元素;列表是

任意长。这样的事情:


class ListElem

{

char Datum [1000];

class ListElem *下一个;

ListElem()

{下一个= NULL; }

~ListElem()

{

class ListElem * el;

while(Next)

{

el = Next;

Next = Next-> Next;

delete el;

}

}

}


但最后一条指令删除el;将对

析构函数进行递归调用,这一切都会失败。


如何在不调用的情况下从析构函数中删除元素

析构函数再次?


谢谢

Timothy Madden

罗马尼亚

---------------------------------------------

我不想错过任何事情

Hello

I have a linked list of object of a class. I thought it would be nice to
have the destructor delete the whole list when I delete just the first
element. I don''t want to recursivly destroy the list elements; the list is
arbitrary long. Something like this:

class ListElem
{
char Datum[1000];
class ListElem *Next;
ListElem()
{ Next = NULL; }
~ListElem()
{
class ListElem *el;
while (Next)
{
el = Next;
Next = Next->Next;
delete el;
}
}
}

but the last instruction delete el; will make a recursive call to the
destructor and it''s all going down.

How can I delete an element, from the destructor, without calling the
destructor again ?

Thank you
Timothy Madden
Romania
---------------------------------------------
And I don''t wanna miss a thing

推荐答案



Timothy Madden < BA **** @ rmv.spam.home.ro>在消息中写道

新闻:2r ************* @ uni-berlin.de ...

"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2r*************@uni-berlin.de...
你好

我有一个类对象的链表。我认为当我删除第一个
元素时,让析构函数删除整个列表会很好。我不想递归破坏列表元素;这个清单是任意长的。这样的事情:


当你说你不想递归地时,我不确定你的意思。

销毁元素。一般来说,你会使用一个循环删除一个列表的'

元素,而不是递归。


(现在,如果你想要*递归,你可以让析构函数只需调用

" delete Next;",这样每个元素都删除了以下元素。

最后一个将调用delete on a NULL指针,这是一个无操作,并且

将结束递归。)类ListElem
{char Datum [1000];
类ListElem *下一个;
ListElem()
{Next = NULL; }
~ListElem()
{ListElem * el;
类while(下一个)
{
el =下一个;
下一个= Next-> Next;
删除el;
}
}
}

但最后一条指令删除了el;将对
析构函数进行递归调用,它会全部消失。

如何从析构函数中删除元素,而无需再次调用
析构函数?
Hello

I have a linked list of object of a class. I thought it would be nice to
have the destructor delete the whole list when I delete just the first
element. I don''t want to recursivly destroy the list elements; the list is
arbitrary long. Something like this:
I''m not sure what you mean when you say you don''t want to "recursively"
destroy the elements. Generally, you''d use a loop to delete a list''s
elements, not recursion.

(Now, if you *wanted* recursion, you could have the destructor just call
"delete Next;", so that each element deleted the following element. The
last one would be calling delete on a NULL pointer, which is a no-op, and
would end the recursion.)

class ListElem
{
char Datum[1000];
class ListElem *Next;
ListElem()
{ Next = NULL; }
~ListElem()
{
class ListElem *el;
while (Next)
{
el = Next;
Next = Next->Next;
delete el;
}
}
}

but the last instruction delete el; will make a recursive call to the
destructor and it''s all going down.

How can I delete an element, from the destructor, without calling the
destructor again ?




我不觉得这个想法,即使你能找到办法做到这一点,也会是一个非常好的方式

做事。为什么不在析构函数外面使用循环*,以便
销毁列表元素。它的最佳位置是析构函数

封装列表本身的类(即拥有指向列表中第一个元素的

指针的那个) )。如果你只有这个列表

住在main,而不是在一个类中,我会建议把它放在一个类

中,然后将循环放入那个班级的'析构者。


-Howard



I don''t think this idea, even if you can find a way to do it, would be a
very good way to do things. Why not use a loop *outside* the destructor, to
destroy the list elements. The best place for it would be in the destructor
of the class that encapsulates the list itself (i.e., the one that owns the
pointer to the first element in the list). If you just have this list
residing in main, and not in a class, I''d recommend putting it in a class
instead, and then putting the loop in that class'' destructor.

-Howard


Timothy Madden写道:
Timothy Madden wrote:
我有一个类对象的链表。我认为当我删除第一个
元素时,让析构函数删除整个列表会很好。我不想递归破坏列表元素;这个清单是任意长的。这样的事情:

类ListElem
{char Datum [1000];
类ListElem * Next;
^^^^^

这里不需要关键字''class'。

ListElem()
{Next = NULL; }
~ListElem()
{ListElem * el类;
^^^^^

这里也不需要关键字''class''。

while(Next)
{
el =下一个;
下一个=下一个 - >下一个;
删除el;
}
}
}

但是最后一条指令删除el;会对
析构函数进行递归调用,而且它都会崩溃。


对析构函数的递归调用没什么大不了的,AFAICT。为了避免

多次删除(我假设你正在经历的事情)你做了什么


~ListElem()

{

删除下一个;

}


这应该照顾整个列表。如果要删除的元素

有''Next''空指针,''删除0''什么都不做,你的递归

返回。

如何在不调用
析构函数的情况下从析构函数中删除元素?
I have a linked list of object of a class. I thought it would be nice to
have the destructor delete the whole list when I delete just the first
element. I don''t want to recursivly destroy the list elements; the list is
arbitrary long. Something like this:

class ListElem
{
char Datum[1000];
class ListElem *Next; ^^^^^
No need for keyword ''class'' here.
ListElem()
{ Next = NULL; }
~ListElem()
{
class ListElem *el; ^^^^^
No need for keyword ''class'' here either.
while (Next)
{
el = Next;
Next = Next->Next;
delete el;
}
}
}

but the last instruction delete el; will make a recursive call to the
destructor and it''s all going down.
A recursive call to the destructor is no big deal, AFAICT. To avoid
multiple deletions (I presume that''s what you''re experiencing) you do

~ListElem()
{
delete Next;
}

That should take care of the whole list. If the element being deleted
has ''Next'' a null pointer, ''delete 0'' does nothing and your recursion
returns.
How can I delete an element, from the destructor, without calling the
destructor again ?




Victor



Victor




" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

news:qR **************** @ newsread1.dllstx09.us.to.v erio.net ...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:qR****************@newsread1.dllstx09.us.to.v erio.net...
Timothy Madden写道:
Timothy Madden wrote:
class ListElem
{char Datum [1000];
class ListElem * Next;
ListElem()
{下一个= NULL; }
~ListElem()
{ListElem * el;
类while(下一个)
{
el =下一个;
下一个= Next-> Next;
删除el;
}
}
}

但最后一条指令删除了el;将对
析构函数进行递归调用,并且它都会失效。
class ListElem
{
char Datum[1000];
class ListElem *Next;
ListElem()
{ Next = NULL; }
~ListElem()
{
class ListElem *el;
while (Next)
{
el = Next;
Next = Next->Next;
delete el;
}
}
}

but the last instruction delete el; will make a recursive call to the
destructor and it''s all going down.



对析构函数的递归调用没什么大不了的,AFAICT。为了避免多次删除(我假设你正在经历的事情)你做了

~ListElem()
{
删除下一个;
}

这应该照顾整个清单。如果被删除的元素
有''Next''空指针,''删除0''什么都不做,你的递归就会返回。



A recursive call to the destructor is no big deal, AFAICT. To avoid
multiple deletions (I presume that''s what you''re experiencing) you do

~ListElem()
{
delete Next;
}

That should take care of the whole list. If the element being deleted
has ''Next'' a null pointer, ''delete 0'' does nothing and your recursion
returns.




我不想递归破坏列表;列表是任意长的。

析构函数中的任何删除下一个将递归调用析构函数;

如果列表是10000或100000或1000000个元素怎么办?



I don''t want to recursively destroy the list; the list is arbitrary long.
Any ''delete Next'' in the desctructor will recursivly call the destructor;
What if the list is 10000 or 100000 or 1000000 elements ?


这篇关于我可以删除而不调用析构函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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