<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.82:不要在構(gòu)造函數(shù)或析構(gòu)函數(shù)中調(diào)用虛函數(shù)

          4c05cb752f01c210f47d5de3b930800b.webp

          7c60affd75394f9d8c58efa715a96f74.webp

          C.82: Don't call virtual functions in constructors and destructors

          C.82:不要在構(gòu)造函數(shù)或析構(gòu)函數(shù)中調(diào)用虛函數(shù)

          0bda0949ef4a5ae38d24eafa7ef08ca7.webp


          Reason(原因)

          The function called will be that of the object constructed so far, rather than a possibly overriding function in a derived class. This can be most confusing. Worse, a direct or indirect call to an unimplemented pure virtual function from a constructor or destructor results in undefined behavior.

          到目前為止,被調(diào)用的函數(shù)應(yīng)該只屬于構(gòu)造對(duì)象本身,而不是可能存在于派生類中的某個(gè)覆蓋函數(shù)。那樣做非常難理解。最壞的情況,在構(gòu)造函數(shù)或者析構(gòu)函數(shù)中直接或間接調(diào)用一個(gè)沒(méi)有實(shí)現(xiàn)的純虛函數(shù)會(huì)導(dǎo)致沒(méi)有定義的行為。


          Example, bad(反面示例)
          class Base {
          public:
          ? ?virtual void f() = 0; ? // not implemented
          ? ?virtual void g(); ? ? ? // implemented with Base version
          ? ?virtual void h(); ? ? ? // implemented with Base version
          ? ?virtual ~Base(); ? ? ? ?// implemented with Base version
          };

          class Derived : public Base {
          public:
          ? ?void g() override; ? // provide Derived implementation
          ? ?void h() final; ? ? ?// provide Derived implementation

          ? ?Derived()
          ? ?{
          ? ? ? ?// BAD: attempt to call an unimplemented virtual function
          ? ? ? ?f();

          ? ? ? ?// BAD: will call Derived::g, not dispatch further virtually
          ? ? ? ?g();

          ? ? ? ?// GOOD: explicitly state intent to call only the visible version
          ? ? ? ?Derived::g();

          ? ? ? ?// ok, no qualification needed, h is final
          ? ? ? ?h();
          ? ?}
          };

          Note that calling a specific explicitly qualified function is not a virtual call even if the function is virtual.

          注意:調(diào)用一個(gè)特定的限定函數(shù)不是虛調(diào)用,即使這個(gè)函數(shù)是虛函數(shù)。

          See alsofactory functions for how to achieve the effect of a call to a derived class function without risking undefined behavior.

          參考工廠函數(shù)以便了解如何達(dá)成調(diào)用派生類功能的效果而不必承擔(dān)引起未定義行為的風(fēng)險(xiǎn)。


          Note(注意)

          There is nothing inherently wrong with calling virtual functions from constructors and destructors. The semantics of such calls is type safe. However, experience shows that such calls are rarely needed, easily confuse maintainers, and become a source of errors when used by novices.

          從構(gòu)造函數(shù)和析構(gòu)函數(shù)中調(diào)用虛函數(shù)并不是本身有什么錯(cuò)誤。這種調(diào)用的語(yǔ)義是安全的。然而,經(jīng)驗(yàn)表明這樣的調(diào)用很少是必須的,很容易擾亂維護(hù)者,如果被新手使用會(huì)成為錯(cuò)誤源。


          Enforcement(實(shí)施建議)
          • Flag calls of virtual functions from constructors and destructors.

          • 提示來(lái)自構(gòu)造函數(shù)或析構(gòu)函數(shù)的虛函數(shù)調(diào)用。

          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c82-dont-call-virtual-functions-in-constructors-and-destructors




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

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

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

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          <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>
                  亚洲欧美日韩动漫 | 日韩操逼图片 | 欧美A片在线免费观看 | 第一页在线视频 | 日本内射视频在线观看 |