<small id='mLFaZ'></small><noframes id='mLFaZ'>

      <i id='mLFaZ'><tr id='mLFaZ'><dt id='mLFaZ'><q id='mLFaZ'><span id='mLFaZ'><b id='mLFaZ'><form id='mLFaZ'><ins id='mLFaZ'></ins><ul id='mLFaZ'></ul><sub id='mLFaZ'></sub></form><legend id='mLFaZ'></legend><bdo id='mLFaZ'><pre id='mLFaZ'><center id='mLFaZ'></center></pre></bdo></b><th id='mLFaZ'></th></span></q></dt></tr></i><div id='mLFaZ'><tfoot id='mLFaZ'></tfoot><dl id='mLFaZ'><fieldset id='mLFaZ'></fieldset></dl></div>

    1. <legend id='mLFaZ'><style id='mLFaZ'><dir id='mLFaZ'><q id='mLFaZ'></q></dir></style></legend>

      <tfoot id='mLFaZ'></tfoot>
        <bdo id='mLFaZ'></bdo><ul id='mLFaZ'></ul>

      可以用“删除这个"吗?删除当前对象?

      Is it OK to use quot;delete thisquot; to delete the current object?(可以用“删除这个吗?删除当前对象?)
        • <bdo id='pTdjq'></bdo><ul id='pTdjq'></ul>
        • <legend id='pTdjq'><style id='pTdjq'><dir id='pTdjq'><q id='pTdjq'></q></dir></style></legend>
          <tfoot id='pTdjq'></tfoot>

          <small id='pTdjq'></small><noframes id='pTdjq'>

            <tbody id='pTdjq'></tbody>
          • <i id='pTdjq'><tr id='pTdjq'><dt id='pTdjq'><q id='pTdjq'><span id='pTdjq'><b id='pTdjq'><form id='pTdjq'><ins id='pTdjq'></ins><ul id='pTdjq'></ul><sub id='pTdjq'></sub></form><legend id='pTdjq'></legend><bdo id='pTdjq'><pre id='pTdjq'><center id='pTdjq'></center></pre></bdo></b><th id='pTdjq'></th></span></q></dt></tr></i><div id='pTdjq'><tfoot id='pTdjq'></tfoot><dl id='pTdjq'><fieldset id='pTdjq'></fieldset></dl></div>

                本文介绍了可以用“删除这个"吗?删除当前对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在编写一个链表,我希望结构的析构函数(一个节点结构)简单地删除自身,并且没有任何副作用.我希望我的列表的析构函数在自身上迭代调用 Node 析构函数(临时存储下一个节点),如下所示:

                I'm writing a linked list and I want a struct's destructor (a Node struct) to simply delete itself, and not have any side effects. I want my list's destructor to iteratively call the Node destructor on itself (storing the next node temporarily), like this:

                //my list class has first and last pointers
                //and my nodes each have a pointer to the previous and next
                //node
                DoublyLinkedList::~DoublyLinkedList
                {
                    Node *temp = first();
                
                    while (temp->next() != NULL)
                    {
                        delete temp;
                        temp = temp->next();
                    }
                }
                

                所以这将是我的 Node 析构函数:

                So this would be my Node destructor:

                Node::~Node
                {
                   delete this;
                }
                

                这是否可以接受,尤其是在这种情况下?

                Is this acceptable, especially in this context?

                推荐答案

                如果 Node 析构函数正在被调用,那么它已经在被删除的过程中.所以在你的 Node 析构函数中删除是没有意义的.

                If the Node destructor is being called, then it's already in the process of being deleted. So a delete doesn't make sense inside your Node destructor.

                这也是错误的:

                while (temp->next() != NULL)
                {
                     delete temp;
                     temp = temp->next();
                }
                

                相反,您应该将 temp->next() 放入临时变量中.否则,您正在访问已删除的内存.

                Instead you should get temp->next() into a temp variable. Otherwise you are accessing deleted memory.

                更像这样:

                DoublyLinkedList::~DoublyLinkedList
                {
                  Node *temp = first();
                  while (temp != NULL)
                  {
                       Node *temp2 = temp->next();
                       delete temp;
                       temp = temp2;
                  }
                }
                

                这篇关于可以用“删除这个"吗?删除当前对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is Type(::x); valid?(是类型(::x);有效的?)
                Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                  <i id='SDvHm'><tr id='SDvHm'><dt id='SDvHm'><q id='SDvHm'><span id='SDvHm'><b id='SDvHm'><form id='SDvHm'><ins id='SDvHm'></ins><ul id='SDvHm'></ul><sub id='SDvHm'></sub></form><legend id='SDvHm'></legend><bdo id='SDvHm'><pre id='SDvHm'><center id='SDvHm'></center></pre></bdo></b><th id='SDvHm'></th></span></q></dt></tr></i><div id='SDvHm'><tfoot id='SDvHm'></tfoot><dl id='SDvHm'><fieldset id='SDvHm'></fieldset></dl></div>

                    • <bdo id='SDvHm'></bdo><ul id='SDvHm'></ul>
                        <tbody id='SDvHm'></tbody>

                    • <small id='SDvHm'></small><noframes id='SDvHm'>

                        <tfoot id='SDvHm'></tfoot>

                          <legend id='SDvHm'><style id='SDvHm'><dir id='SDvHm'><q id='SDvHm'></q></dir></style></legend>