• <tfoot id='8exQ9'></tfoot>

    <legend id='8exQ9'><style id='8exQ9'><dir id='8exQ9'><q id='8exQ9'></q></dir></style></legend>
      • <bdo id='8exQ9'></bdo><ul id='8exQ9'></ul>

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

        <small id='8exQ9'></small><noframes id='8exQ9'>

      1. 覆盖纯虚函数的参数数量

        Override number of parameters of pure virtual functions(覆盖纯虚函数的参数数量)
          • <tfoot id='wZ8T8'></tfoot>
              • <bdo id='wZ8T8'></bdo><ul id='wZ8T8'></ul>

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

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

                  本文介绍了覆盖纯虚函数的参数数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经实现了以下接口:

                  I have implemented the following interface:

                  template <typename T>
                  class Variable
                  {
                  public:
                    Variable (T v) : m_value (v) {}
                    virtual void Callback () = 0;
                  private:
                    T m_value;
                  };
                  

                  一个合适的派生类可以这样定义:

                  A proper derived class would be defined like this:

                  class Derived : public Variable<int>
                  {
                  public:
                    Derived (int v) : Variable<int> (v) {}
                    void Callback () {}
                  };
                  

                  但是,我想派生 Callback 接受不同参数的类(例如:void Callback (int a, int b)).有办法吗?

                  However, I would like to derive classes where Callback accepts different parameters (eg: void Callback (int a, int b)). Is there a way to do it?

                  推荐答案

                  这个问题我遇到过很多次了.

                  This is a problem I ran in a number of times.

                  这是不可能的,并且有充分的理由,但有一些方法可以实现基本相同的目标.就个人而言,我现在使用:

                  This is impossible, and for good reasons, but there are ways to achieve essentially the same thing. Personally, I now use:

                  struct Base
                  {
                    virtual void execute() = 0;
                    virtual ~Base {}
                  };
                  
                  class Derived: public Base
                  {
                  public:
                    Derived(int a, int b): mA(a), mB(b), mR(0) {}
                  
                    int getResult() const { return mR; }
                  
                    virtual void execute() { mR = mA + mB; }
                  
                  private:
                    int mA, mB, mR;
                  };
                  

                  在行动:

                  int main(int argc, char* argv[])
                  {
                    std::unique_ptr<Base> derived(new Derived(1,2));
                    derived->execute();
                    return 0;
                  } // main
                  

                  这篇关于覆盖纯虚函数的参数数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                    <bdo id='YIU1N'></bdo><ul id='YIU1N'></ul>
                      <tbody id='YIU1N'></tbody>

                    <legend id='YIU1N'><style id='YIU1N'><dir id='YIU1N'><q id='YIU1N'></q></dir></style></legend>
                      1. <small id='YIU1N'></small><noframes id='YIU1N'>

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