1. <legend id='gMZp0'><style id='gMZp0'><dir id='gMZp0'><q id='gMZp0'></q></dir></style></legend>

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

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

      3. <tfoot id='gMZp0'></tfoot>

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

        所有的虚函数都需要在派生类中实现吗?

        时间:2023-08-02
          <tbody id='k19C7'></tbody>
        <legend id='k19C7'><style id='k19C7'><dir id='k19C7'><q id='k19C7'></q></dir></style></legend>
          <i id='k19C7'><tr id='k19C7'><dt id='k19C7'><q id='k19C7'><span id='k19C7'><b id='k19C7'><form id='k19C7'><ins id='k19C7'></ins><ul id='k19C7'></ul><sub id='k19C7'></sub></form><legend id='k19C7'></legend><bdo id='k19C7'><pre id='k19C7'><center id='k19C7'></center></pre></bdo></b><th id='k19C7'></th></span></q></dt></tr></i><div id='k19C7'><tfoot id='k19C7'></tfoot><dl id='k19C7'><fieldset id='k19C7'></fieldset></dl></div>
          • <tfoot id='k19C7'></tfoot>

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

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

                • 本文介绍了所有的虚函数都需要在派生类中实现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这似乎是一个简单的问题,但我在其他任何地方都找不到答案.

                  This may seem like a simple question, but I can't find the answer anywhere else.

                  假设我有以下内容:

                  class Abstract {
                  public:
                      virtual void foo() = 0;
                      virtual void bar();
                  }
                  
                  class Derived : Abstract {
                  public:
                      virtual void foo();
                  }
                  

                  Derived 类不实现 bar() 函数可以吗?如果不是我的所有派生类都需要 bar() 函数,但有些需要,该怎么办.抽象基类的所有虚函数都需要在派生类中实现,还是只需要在纯虚函数中实现?谢谢

                  Is it ok that class Derived does not implement the bar() function? What if not ALL of my derived classes need the bar() function, but some do. Do all of the virtual functions of an abstract base class need to be implemented in the derived classes, or just the ones that are pure virtual? Thanks

                  推荐答案

                  派生类不必自己实现所有虚函数.他们只需要实现 pure 的那些.1 这意味着问题中的 Derived 类是正确的.它继承其祖先类Abstractbar实现.(这假设 Abstract::bar 在某处实现.问题中的代码声明了该方法,但没有定义它.您可以将其定义为 Trenki's answer 显示,或者您可以单独定义它.)

                  Derived classes do not have to implement all virtual functions themselves. They only need to implement the pure ones.1 That means the Derived class in the question is correct. It inherits the bar implementation from its ancestor class, Abstract. (This assumes that Abstract::bar is implemented somewhere. The code in the question declares the method, but doesn't define it. You can define it inline as Trenki's answer shows, or you can define it separately.)

                  1 即便如此,仅当派生类要实例化时.如果派生类没有直接实例化,而只是作为更多派生类的基类存在,那么负责实现所有纯虚方法的是那些那些类.层次结构中的中间"类允许保留一些未实现的纯虚拟方法,就像基类一样.如果中间"类确实实现了一个纯虚方法,那么它的后代将继承该实现,因此他们不必自己重新实现它.

                  1 And even then, only if the derived class is going to be instantiated. If a derived class is not instantiated directly, but only exists as a base class of more derived classes, then it's those classes that are responsible for having all their pure virtual methods implemented. The "middle" class in the hierarchy is allowed to leave some pure virtual methods unimplemented, just like the base class. If the "middle" class does implement a pure virtual method, then its descendants will inherit that implementation, so they don't have to re-implement it themselves.

                  这篇关于所有的虚函数都需要在派生类中实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 构造函数/析构函数继承 下一篇:不能因为类不是多态而沮丧吗?

                  相关文章

                  最新文章

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

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

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