链表析构函数
我正在利用自己的时间学习 C++,并编写一个链表来尝试掌握它的窍门。我担心我删除该对象的方式。这是一个单链表。这是析构函数:
template <typename T>
LinkedList<T>::~LinkedList()
{
Node<T> * current = this->first;
do {
Node * temp = current->next;
delete current; // THIS JUST MIGHT BE A TERRIBLE IDEA!!!
Node * current = temp; // new current-- might work with the current
// delete a line above
} while (current->next != 0); // need to leave this->last so that I don't
// delete it twice in the next line.
// Just realized I'm deleting this->first, then
// in the next line [implicitly] deleting it again!
//
delete this;
}
我创建一个指向列表中第一个节点的指针,创建一个指向下一个节点的临时指针,删除第一个指针,创建一个具有相同名称的新指针,然后循环返回。完成后,它删除“this”指针。
我确信您可以明白为什么我对创建与已删除指针同名的新指针的方式感到担心。
I'm studying C++ on my own time, and writing a linked list to try and get the hang of it. I'm worried about the way I've come up to delete the object. It's a singly linked list. Here's the destructor:
template <typename T>
LinkedList<T>::~LinkedList()
{
Node<T> * current = this->first;
do {
Node * temp = current->next;
delete current; // THIS JUST MIGHT BE A TERRIBLE IDEA!!!
Node * current = temp; // new current-- might work with the current
// delete a line above
} while (current->next != 0); // need to leave this->last so that I don't
// delete it twice in the next line.
// Just realized I'm deleting this->first, then
// in the next line [implicitly] deleting it again!
//
delete this;
}
I create a pointer to the first node in the list, create a temporary pointer to the next node, delete the first pointer, create a new pointer with the same name, which then loops back. After it's done, it deletes the 'this' pointer.
I'm sure you can see why I'm worried with the way I create a new pointer with the same name as a deleted pointer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
删除它
。Node
是模板,那么您需要在所有这些定义中编写Node
。current
,只需为其分配一个新值。除此之外,我在这段代码中没有看到任何其他问题。
delete this
in the destructor.Node
is a template, then you need to writeNode<T>
in all of those definitions.current
, just assign it a new value.Other than that, I don't see any other problems in this snippet.
为什么不编译代码,尝试一下,看看会发生什么?最糟糕的事情是你的程序崩溃了,你必须找出原因。
您的代码基本上应该可以工作,只是您需要在 while 循环条件中测试
current
而不是current->next
并且编写current->next
是多余的(并且可能是错误的) code>delete this 在析构函数中,还有一些 Cat Plus Plus 在他的回答中指出的错误。如果您正在尝试学习 C++,那么您应该学习更多内容,直到您了解此处所犯的错误,并确信固定代码能够正常工作。
这是我的函数的固定版本:
Why not compile the code, try it, and see what happens? The worst thing that would happen would be that your program crashes and you have to figure out why.
Your code should basically work except that you need to test
current
in the while loop condition instead ofcurrent->next
and it is redundant (and probably wrong) to writedelete this
in a destructor, and there are some more errors that Cat Plus Plus pointed out in his answer.If you're trying to learn C++, then you should learn more of it to the point where you understand the mistakes you made here and are confident that the fixed code will work.
Here is my fixed version of the function:
我没有看到这个问题,但我看到很多错误:
正确的代码是这样的:
I don't see the question but I see lots of errors:
The correct code is this:
析构函数中的
delete this;
就像代码自杀。您的对象已经被破坏,并且您再次使用delete this;
进行破坏。这是一种未定义的行为。你可以删除它。其余的事情看起来都很好(假设this->first
给出了头Node
)。编辑:我错过了,你重新定义了
当前
。删除它。 (应该简单地current = temp;
)delete this;
in a destructor is like a code-suicide. Your object is already getting destructed and you are again destructing withdelete this;
. It's an undefined behavior. You can remove that. Rest of things look fine (assuming thatthis->first
gives the headNode
).Edit: I missed that, you have redefined
current
. Remove that. (should be simplycurrent = temp;
)