• <legend id='fY3G1'><style id='fY3G1'><dir id='fY3G1'><q id='fY3G1'></q></dir></style></legend>
      <bdo id='fY3G1'></bdo><ul id='fY3G1'></ul>

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

    1. <tfoot id='fY3G1'></tfoot>

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

        delete 是否适用于指向基类的指针?

        时间:2023-07-20
          <tfoot id='KCJOp'></tfoot>

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

              <tbody id='KCJOp'></tbody>
              1. <i id='KCJOp'><tr id='KCJOp'><dt id='KCJOp'><q id='KCJOp'><span id='KCJOp'><b id='KCJOp'><form id='KCJOp'><ins id='KCJOp'></ins><ul id='KCJOp'></ul><sub id='KCJOp'></sub></form><legend id='KCJOp'></legend><bdo id='KCJOp'><pre id='KCJOp'><center id='KCJOp'></center></pre></bdo></b><th id='KCJOp'></th></span></q></dt></tr></i><div id='KCJOp'><tfoot id='KCJOp'></tfoot><dl id='KCJOp'><fieldset id='KCJOp'></fieldset></dl></div>
              2. <legend id='KCJOp'><style id='KCJOp'><dir id='KCJOp'><q id='KCJOp'></q></dir></style></legend>
                • <bdo id='KCJOp'></bdo><ul id='KCJOp'></ul>
                  本文介绍了delete 是否适用于指向基类的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  你是否必须传递与 new 返回的相同的指针,或者你是否可以传递一个指向类基类型之一的指针?例如:

                  Do you have to pass delete the same pointer that was returned by new, or can you pass it a pointer to one of the classes base types? For example:

                  class Base
                  {
                  public:
                      virtual ~Base();
                      ...
                  };
                  
                  class IFoo
                  {
                  public:
                      virtual ~IFoo() {}
                      virtual void DoSomething() = 0;
                  };
                  
                  class Bar : public Base, public IFoo
                  {
                  public:
                      virtual ~Bar();
                      void DoSomething();
                      ...
                  };
                  
                  Bar * pBar = new Bar;
                  IFoo * pFoo = pBar;
                  delete pFoo;
                  

                  当然这大大简化了.我真正想做的是创建一个充满 boost::shared_ptr 的容器,并将其传递给一些代码,以便在完成后将其从容器中删除.这段代码对 Bar 或 Base 的实现一无所知,将依赖于 shared_ptr 析构函数中隐含的删除操作符来做正确的事情.

                  Of course this is greatly simplified. What I really want to do is create a container full of boost::shared_ptr and pass it to some code that will remove it from the container when it is finished. This code will know nothing of the implementation of Bar or Base, and will rely on the implied delete operator in the shared_ptr destructor to do the right thing.

                  这可能行得通吗?我的直觉说不,因为指针不会有相同的地址.另一方面, dynamic_cast 应该可以工作,所以编译器在某处存储了足够的信息来解决它.<小时>感谢所有回答和评论的人的帮助.我已经知道虚拟析构函数的重要性,如我的示例所示;看到答案后,我稍微思考了一下,并意识到虚拟析构函数的全部原因就是这种情况.因此它必须起作用.由于没有将指针转换回原始指针的可见方法,我感到很困惑.多一点思考让我相信有一种无形的手段,我推论析构函数正在返回真正的指针以供 delete 释放.当我在 ~Base 中看到这一行时,调查来自 Microsoft VC++ 的编译代码证实了我的怀疑:

                  Can this possibly work? My intuition says no, since the pointers will not have the same address. On the other hand, a dynamic_cast<Bar*> should work, so somewhere the compiler is storing enough information to figure it out.


                  Thanks for the help, everybody who answered and commented. I already knew the importance of virtual destructors, as shown in my example; after seeing the answer I gave it a little thought, and realized the whole reason for a virtual destructor is this exact scenario. Thus it had to work. I was thrown by the absence of a visible means of converting the pointer back to the original. A little more thinking led me to believe there was an invisible means, and I theorized that the destructor was returning the true pointer for delete to release. Investigating the compiled code from Microsoft VC++ confirmed my suspicion when I saw this line in ~Base:

                  mov eax, DWORD PTR _this$[ebp]
                  

                  跟踪汇编程序发现这是传递给删除函数的指针.谜底解开.

                  Tracing the assembler revealed that this was the pointer being passed to the delete function. Mystery solved.

                  我已经修复了将虚拟析构函数添加到 IFoo 的示例,这是一个简单的疏忽.再次感谢所有指出这一点的人.

                  I've fixed the example to add the virtual destructor to IFoo, it was a simple oversight. Thanks again to everyone who pointed it out.

                  推荐答案

                  是的,它会起作用,当且仅当基类析构函数是虚拟的,你已经为 做了Base 基类,但不适用于 IFoo 基类.如果基类析构函数是虚拟的,那么当你在基类指针上调用operator delete时,它使用动态调度通过在虚函数中查找派生类析构函数来找出如何删除对象表.

                  Yes, it will work, if and only if the base class destructor is virtual, which you have done for the Base base class but not for the IFoo base class. If the base class destructor is virtual, then when you call operator delete on the base class pointer, it uses dynamic dispatch to figure out how to delete the object by looking up the derived class destructor in the virtual function table.

                  在你的多重继承的情况下,只有当你删除它的基类有一个虚拟析构函数时它才会工作;其他基类没有虚拟析构函数是可以的,但前提是您不尝试通过那些其他基类指针删除任何派生对象.

                  In your case of multiple inheritance, it will only work if the base class you're deleting it through has a virtual destructor; it's ok for the other base classes to not have a virtual destructor, but only if you don't try to delete any derived objects via those other base class pointers.

                  这篇关于delete 是否适用于指向基类的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是 boost shared_ptr &lt;XXX&gt;线程安全? 下一篇:使用 boost 创建线程池

                  相关文章

                  • <bdo id='I1u2R'></bdo><ul id='I1u2R'></ul>
                • <tfoot id='I1u2R'></tfoot>

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

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