<tfoot id='83ITN'></tfoot>
    1. <legend id='83ITN'><style id='83ITN'><dir id='83ITN'><q id='83ITN'></q></dir></style></legend>

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

      <small id='83ITN'></small><noframes id='83ITN'>

          <bdo id='83ITN'></bdo><ul id='83ITN'></ul>

        为什么我们在 C++ 中实际上需要 Private 或 Protect

        时间:2023-08-02

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

              1. <small id='q7n3i'></small><noframes id='q7n3i'>

                  <legend id='q7n3i'><style id='q7n3i'><dir id='q7n3i'><q id='q7n3i'></q></dir></style></legend>
                    <tbody id='q7n3i'></tbody>

                  本文介绍了为什么我们在 C++ 中实际上需要 Private 或 Protected 继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 C++ 中,我想不出我想从一个继承私有/保护的情况基类:

                  In C++, I can't think of a case in which I would like to inherit private/protected from a base class:

                  class Base;
                  class Derived1 : private Base;
                  class Derived2 : protected Base;
                  

                  真的有用吗?

                  推荐答案

                  当您想要访问基类的某些成员,但又不想在类接口中公开它们时,它很有用.私有继承也可以看作是某种组合:C++ faq-lite 给出下面的例子来说明这个语句

                  It is useful when you want to have access to some members of the base class, but without exposing them in your class interface. Private inheritance can also be seen as some kind of composition: the C++ faq-lite gives the following example to illustrate this statement

                  class Engine {
                   public:
                     Engine(int numCylinders);
                     void start();                 // Starts this Engine
                  };
                  
                  class Car {
                    public:
                      Car() : e_(8) { }             // Initializes this Car with 8 cylinders
                      void start() { e_.start(); }  // Start this Car by starting its Engine
                    private:
                      Engine e_;                    // Car has-a Engine
                  };
                  

                  为了获得相同的语义,您也可以将汽车类编写如下:

                  To obtain the same semantic, you could also write the car Class as follow:

                  class Car : private Engine {    // Car has-a Engine
                   public:
                     Car() : Engine(8) { }         // Initializes this Car with 8 cylinders
                     using Engine::start;          // Start this Car by starting its Engine
                  }; 
                  

                  然而,这种做法有几个缺点:

                  However, this way of doing has several disadvantages:

                  • 你的意图不太清楚
                  • 它可能导致滥用多重继承
                  • 它破坏了 Engine 类的封装,因为您可以访问其受保护的成员
                  • 您可以覆盖引擎虚拟方法,如果您的目标是一个简单的组合,这是您不想要的

                  这篇关于为什么我们在 C++ 中实际上需要 Private 或 Protected 继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 C++ 中,是否可以将类声明为从另一个类继承 下一篇:静态字段是否继承?

                  相关文章

                  最新文章

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

                      • <bdo id='jTL6k'></bdo><ul id='jTL6k'></ul>

                      <tfoot id='jTL6k'></tfoot>
                    2. <legend id='jTL6k'><style id='jTL6k'><dir id='jTL6k'><q id='jTL6k'></q></dir></style></legend>