简体   繁体   English

C ++如何从基类调用派生类中的方法

[英]C++ how to call method in derived class from base class

What I want to do is for Execute() to run and completes it calls the Base::Done() then calls the Derived::Done() . 我想要做的是让Execute()运行并完成它调用Base::Done()然后调用Derived::Done() I'm doing this because Base class Execute will do something and when its done call the Derived::Done() . 我这样做是因为BaseExecute会做一些事情,当它完成时调用Derived::Done() I hope I'm explaining it correctly. 我希望我能正确解释。 Kind of like a listener that is called when a task completed. 有点像任务完成时调用的监听器。 I'm kinda stuck on how the Base class will call the Derived class. 我有点坚持Base类将如何调用Derived类。

class Base
{
  virtual void Done(int code){};
  void Execute();
}

void Base::Execute()
{
}


class Derived : Base
{
  void Done(int code);
  void Run();
}

Derived::Done(int code)
{
}

void Derived::Run()
{
    Execute();
}

You can use a template method: 您可以使用模板方法:

class Base
{
 public:
  void Execute()
  {
    BaseDone(42);
    DoDone(42);
  }
 private:
  void BaseDone(int code){};
  virtual void DoDone(int) = 0;
};

class Derived : Base
{
 public:
  void Run() { Execute(); }
 private:
  void DoDone(int code) { .... }
};

Here, Base controls how its own and derived methods are used in Execute() , and the derived types only have to implement one component of that implementation via a private virtual method DoDone() . 这里, Base控制如何在Execute()中使用自己的派生方法和派生方法,派生类型只需要通过私有虚方法DoDone()实现该实现的一个组件。

The base class method can call the derived method quite simply: 基类方法可以非常简单地调用派生方法:

void Base::Execute()
{
    Done(42);
}

To have the base class Done() called before the derived class, you can either call it as the first statement in the derived class method, or use the non-virtual idiom. 要在派生类之前调用​​基类Done(),可以将其作为派生类方法中的第一个语句调用,或者使用非虚拟习语。

Here's an example of calling it at the top of the derived class method. 这是在派生类方法的顶部调用它的示例。 This relies on the derived class to get it right. 这依赖于派生类来使其正确。

void Derived::Done(int code)
{
    Base::Done(code);
}

Here's an example of using the non-virtual idiom: 以下是使用非虚拟习语的示例:

class Base
{
    void Done(int code){
        // Do whatever
        DoneImpl(); // Do derived work.
    }
    virtual void DoneImpl() { };
    ...
 };

 class Derived {
     virtual void DoneImpl() { 
         // Do derived class work.
     };
     ...
 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以从C ++的派生类中调用基类中的方法? - is it possible to call a method in the base class from a derived class in c++? 使用C ++,如何从派生类方法调用基类方法,并将其应用于作为参数传递的对象? - Using C++, how to call a base class method from a derived class method and apply this to an object passed as an argument? C ++如何从多个派生类调用基类方法? - C++ how to call Base class method from multiple derived class? C ++如何从main调用派生的基类函数 - C++ How to call a derived base class function from main C ++如何首先从基类然后从派生调用同一方法? - C++ How call the same method firstly from base class, then from derived? 如何从派生类中的方法从基类调用方法? - How to call a method from a base class from a method in a derived class? 从基类类型的指针数组中调用派生类的方法。 (C ++) - Call a method of a derived class from an array of pointers of type base class. (C++) C ++没有匹配函数可以从派生类调用基类方法 - c++ no matching function for call to base-class method from derived-class C++ 不能从派生的 class 内部调用基础 class 方法 - C++ Cannot call base class method from within derived class 基类c ++中可互换的派生类方法 - interchangeable derived class method from base class c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM