链表析构函数

时间:2011-07-04 05:56:22

标签: c++ linked-list destructor delete-operator

我正在自己的时间学习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'指针。

我确定你可以理解为什么我担心我创建一个与删除指针同名的新指针的方式。

4 个答案:

答案 0 :(得分:1)

  1. 请勿在析构函数中delete this
  2. 如果Node是模板,那么您需要在所有这些定义中编写Node<T>
  3. 请勿重新定义current,只需为其指定新值。
  4. 除此之外,我在这个片段中没有看到任何其他问题。

答案 1 :(得分:1)

为什么不编译代码,尝试一下,看看会发生什么?最糟糕的事情是你的程序崩溃,你必须找出原因。

您的代码基本上应该工作,除了您需要在while循环条件而不是current中测试current->next并且在析构函数中编写delete this是多余的(并且可能是错误的) ,Cat Plus Plus在他的回答中指出了一些错误。

如果您正在尝试学习C ++,那么您应该了解更多信息,以便了解您在此处所犯的错误,并确信固定代码能够正常运行。

这是我的功能的固定版本:

template <typename T> LinkedList<T>::~LinkedList() 
{
    Node<T> * current = this->first;
    while(current != 0) {
        Node<T> * temp = current->next;
        delete current;
        current = temp;
    }
    delete this;
}

答案 2 :(得分:1)

我没有看到这个问题,但我看到很多错误:

template <typename T>
LinkedList<T>::~LinkedList() 
{
    Node<T>* current = this->first; // you never check if this->first is 0
    do {
        Node * temp = current->next;
        delete current; // THIS is not a problem
        Node * current = temp; /* this line has no effect -
                     you define new variable and it disappears when reaches
                     the end of its scope next line */
    } while (current->next != 0); /* 'current' is always 'this->first' but
              even if you would assign it 'temp' like you intended few lines
              above you never check if 'current' is 0 so you will
              dereference 0 when you reach the end of the list */

    delete this; /* this line is total nonsense
            if your LinkedList is created with 'new LinkedList' then
            you have infinite recursion (you call destructor from destructor)
            otherwise you 'delete' pointer that you never 'new'-ed */
}

正确的代码是:

template <typename T>
LinkedList<T>::~LinkedList() 
{
    Node<T>* current = this->first;
    while (current != 0)
    {
        Node<T>* temp = current->next;
        delete current;
        current = temp;
    }
}

答案 3 :(得分:0)

~LinkedList
{
//...
  delete this;
}
析构函数中的

delete this;就像代码自杀。您的对象已经被破坏,您再次使用delete this;进行破坏。这是一个未定义的行为。你可以删除它。其余的事情看起来很好(假设this->first给出头部Node)。

修改:我错过了,您重新定义了current。删除它。 (应该只是current = temp;