What’s the recommended way to invoke callback function defined in container class from contained class?

Viewed 1164

Imagine you had a class hierarchy like the following:

class Robot
{
public:
    void OnTaskCompleted() {}

private:
    Task *m_pTask;
};

class Task
{
public:
    virtual void DoTask() = 0;
};

class TidyUp : public Task
{
public:
    void DoTask()
    {
        // When TidyUp task is compeleted invoke OnTaskCompleted() from here.
    }
};

I need to call OnTaskCompleted() from TidyUp::DoTask(). What would be the recommended way to do that?

I'd like to avoid:

  • making OnTaskCompleted() static
  • passing Robot pointer to Task
4 Answers
Related