<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.52:合理使用繼承的構(gòu)造函數(shù)

          45266857776f1af1e5560b385766d836.webp

          C.52: Use inheriting constructors to import constructors into a derived class that does not need further explicit initialization
          C.52:使用繼承的構(gòu)造函數(shù)功能將構(gòu)造函數(shù)導(dǎo)入不再需要進(jìn)一步明確初始化的派生類(lèi)


          Reason(原因)

          If you need those constructors for a derived class, re-implementing them is tedious and error-prone.

          如果派生類(lèi)需要那些構(gòu)造函數(shù),重新實(shí)現(xiàn)它們的工作單調(diào)乏味而且容易發(fā)生錯(cuò)誤。


          Example(示例)

          std::vector has a lot of tricky constructors, so if I want my own vector, I don't want to reimplement them:

          std::vector有大量的構(gòu)造函數(shù)很難用,因此如果我需要自己的vector,我不會(huì)重新實(shí)現(xiàn)它們。

          class Rec {
          ? ?// ... data and lots of nice constructors ...
          };

          class Oper : public Rec {
          ? ?using Rec::Rec;
          ? ?// ... no data members ...
          ? ?// ... lots of nice utility functions ...
          };

          Example, bad(反面示例)
          struct Rec2 : public Rec {
          ? ?int x;
          ? ?using Rec::Rec;
          };

          Rec2 r {"foo", 7};
          int val = r.x; ? // uninitialized
          b7ac9532df2baa2141608661e4981af7.webp

          這就是需要進(jìn)一步初始化的例子。如果派生類(lèi)沒(méi)有增加數(shù)據(jù)成員只是增加一些功能,就可以使用using Rec::Rec這種方法導(dǎo)入基類(lèi)的構(gòu)造函數(shù)。對(duì)于上面的例子也可以考慮使用類(lèi)內(nèi)初始化器初始化數(shù)據(jù)成員x。

          c8cd7eb80bbe3e48b7bd0096a32601da.webp


          Enforcement

          Make sure that every member of the derived class is initialized.

          保證派生類(lèi)的所有成員都被初始化。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c52-use-inheriting-constructors-to-import-constructors-into-a-derived-class-that-does-not-need-further-explicit-initialization




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

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

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

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          C++核心準(zhǔn)則C.51:使用委托構(gòu)造函數(shù)實(shí)現(xiàn)所有構(gòu)造函數(shù)的共通動(dòng)作
          C++核心準(zhǔn)則C.50:如果在構(gòu)造過(guò)程中需要“虛行為”,使用工廠函數(shù)
          <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>
                  日韩AV中文字幕在线免费观看 | 一级黄色H片在线播放 | 综合亚洲色区 | 欧美特大黄 | 超碰99精品 |