<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準(zhǔn)則C.35:基類的析構(gòu)函數(shù)要么是公開的虛函數(shù),要么是保護(hù)...

          c62c7014bab1519faab8dc1f9a96cd3f.webpC.35: A base class destructor should be either public and virtual, or protected and nonvirtual

          基類的析構(gòu)函數(shù)要么是公開的虛函數(shù),要么是保護(hù)的非虛函數(shù)


          Reason(原因)d3d3e993f6039595c20d8ab97bd74c79.webp
          To prevent undefined behavior. If the destructor is public, then calling code can attempt to destroy a derived class object through a base class pointer, and the result is undefined if the base class's destructor is non-virtual. If the destructor is protected, then calling code cannot destroy through a base class pointer and the destructor does not need to be virtual; it does need to be protected, not private, so that derived destructors can invoke it. In general, the writer of a base class does not know the appropriate action to be done upon destruction.

          為了避免無(wú)定義的行為。如果析構(gòu)函數(shù)是公有的,那么調(diào)用側(cè)的代碼就會(huì)嘗試使用基類指針銷毀派生類的對(duì)象,在基類的析構(gòu)函數(shù)為非虛函數(shù)時(shí)其結(jié)果時(shí)沒(méi)有定義的。如果析構(gòu)函數(shù)時(shí)保護(hù)的,那么調(diào)用側(cè)代碼就無(wú)法通過(guò)基類類型指針銷毀派生類對(duì)象,這是析構(gòu)函數(shù)就沒(méi)有必要一定是虛函數(shù)。析構(gòu)函數(shù)是保護(hù)而不是私有的,這樣派生類的析構(gòu)函數(shù)才能調(diào)用它。通常,基類的設(shè)計(jì)者不會(huì)知道在析構(gòu)函數(shù)中應(yīng)該執(zhí)行什么樣的動(dòng)作。


          Discussion(討論)d3d3e993f6039595c20d8ab97bd74c79.webp
          See this in the Discussion section:參見討論章節(jié):

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Sd-dtor.


          Example, bad(反面示例)d3d3e993f6039595c20d8ab97bd74c79.webpstruct Base { ?// BAD: implicitly has a public nonvirtual destructor
          ? ?virtual void f();
          };

          struct D : Base {
          ? ?string s {"a resource needing cleanup"};
          ? ?~D() { /* ... do some cleanup ... */ }
          ? ?// ...
          };

          void use()
          {
          ? ?unique_ptr p = make_unique();
          ? ?// ...
          } // p's destruction calls ~Base(), not ~D(), which leaks D::s and possibly more

          左右滑動(dòng)查看更多


          Note(注意)d3d3e993f6039595c20d8ab97bd74c79.webp

          A virtual function defines an interface to derived classes that can be used without looking at the derived classes. If the interface allows destroying, it should be safe to do so.

          虛函數(shù)定義了派生類的接口,它可以在不關(guān)注派生類的情況下使用。如果接口允許對(duì)象,那么這個(gè)銷毀過(guò)程應(yīng)該是安全的。


          Note(注意)d3d3e993f6039595c20d8ab97bd74c79.webp
          A destructor must be nonprivate or it will prevent using the type:

          析構(gòu)函數(shù)必須是非私有的,除了它不想被別人用。(這樣可以由類自己控制銷毀,譯者注)

          class X {

          ? ?~X(); ? // private destructor
          ? ?// ...
          };

          void use()
          {
          ? ?X a; ? ? ? ? ? ? ? ? ? ? ? ?// error: cannot destroy
          ? ?auto p = make_unique(); ?// error: cannot destroy
          }



          Exception(例外)d3d3e993f6039595c20d8ab97bd74c79.webp

          We can imagine one case where you could want a protected virtual destructor: When an object of a derived type (and only of such a type) should be allowed to destroy another object (not itself) through a pointer to base. We haven't seen such a case in practice, though.

          我們可以想象一種需要保護(hù)的虛函數(shù)析構(gòu)函數(shù)的情況:當(dāng)希望允許派生類的對(duì)象(只有這個(gè)類型)通過(guò)基類指針銷毀另外一個(gè)對(duì)象(不是它自己)時(shí)。但是我們還沒(méi)有在實(shí)際的開發(fā)中遇到這種情況。


          Enforcement(實(shí)施建議)d3d3e993f6039595c20d8ab97bd74c79.webp
          • A class with any virtual functions should have a destructor that is either public and virtual or else protected and nonvirtual.
          • 擁有虛函數(shù)的類的虛函數(shù)要么是公開的虛函數(shù),要么是保護(hù)的非虛函數(shù)。


          譯者注:擁有虛函數(shù)一般就意味著它有派生類。


          原文鏈接:d3d3e993f6039595c20d8ab97bd74c79.webp

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c35-a-base-class-destructor-should-be-either-public-and-virtual-or-protected-and-nonvirtual




          覺(jué)得本文有幫助?請(qǐng)分享給更多人。

          關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>


          瀏覽 42
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  中文字幕第二页 | 青青操青青操在线视频免费 | 4438成人在线 | 人妻久久精品天天中文字幕 | 日韩色视频在线观看 |