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

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

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

      2. 为什么要在 C++ 中为抽象类声明虚拟析构函数?

        时间:2023-08-02
            <tbody id='67lud'></tbody>
          <tfoot id='67lud'></tfoot>

          <small id='67lud'></small><noframes id='67lud'>

            <legend id='67lud'><style id='67lud'><dir id='67lud'><q id='67lud'></q></dir></style></legend>
              <bdo id='67lud'></bdo><ul id='67lud'></ul>

                  <i id='67lud'><tr id='67lud'><dt id='67lud'><q id='67lud'><span id='67lud'><b id='67lud'><form id='67lud'><ins id='67lud'></ins><ul id='67lud'></ul><sub id='67lud'></sub></form><legend id='67lud'></legend><bdo id='67lud'><pre id='67lud'><center id='67lud'></center></pre></bdo></b><th id='67lud'></th></span></q></dt></tr></i><div id='67lud'><tfoot id='67lud'></tfoot><dl id='67lud'><fieldset id='67lud'></fieldset></dl></div>
                  本文介绍了为什么要在 C++ 中为抽象类声明虚拟析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道在 C++ 中为基类声明虚拟析构函数是一种很好的做法,但是即使对于用作接口的抽象类,声明 virtual 析构函数总是很重要吗?请提供一些原因和示例.

                  I know it is a good practice to declare virtual destructors for base classes in C++, but is it always important to declare virtual destructors even for abstract classes that function as interfaces? Please provide some reasons and examples why.

                  推荐答案

                  对于界面来说,这一点更为重要.你的类的任何用户都可能持有一个指向接口的指针,而不是一个指向具体实现的指针.当他们来删除它时,如果析构函数是非虚拟的,他们将调用接口的析构函数(或编译器提供的默认值,如果您没有指定),而不是派生类的析构函数.即时内存泄漏.

                  It's even more important for an interface. Any user of your class will probably hold a pointer to the interface, not a pointer to the concrete implementation. When they come to delete it, if the destructor is non-virtual, they will call the interface's destructor (or the compiler-provided default, if you didn't specify one), not the derived class's destructor. Instant memory leak.

                  例如

                  class Interface
                  {
                     virtual void doSomething() = 0;
                  };
                  
                  class Derived : public Interface
                  {
                     Derived();
                     ~Derived() 
                     {
                        // Do some important cleanup...
                     }
                  };
                  
                  void myFunc(void)
                  {
                     Interface* p = new Derived();
                     // The behaviour of the next line is undefined. It probably 
                     // calls Interface::~Interface, not Derived::~Derived
                     delete p; 
                  }
                  

                  这篇关于为什么要在 C++ 中为抽象类声明虚拟析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么在虚拟继承中调用默认构造函数? 下一篇:防止 C++ 中的类继承

                  相关文章

                  最新文章

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

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

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

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