Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8416649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:49:18+00:00 2026-06-10T01:49:18+00:00

Imagine you had a class hierarchy like the following: class Robot { public: void

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:49:20+00:00Added an answer on June 10, 2026 at 1:49 am

    The static route is not feasible unless there is only one Robot in your program, and an instance of that robot is available statically.

    Passing a Robot to the task may be OK, but it might reveal too much information and prohibit task usages with objects other than robots.

    A third alternative would be to make an interface-like class for completion notifications, extending it in the Robot, and calling it from the task. Unfortunately, C++ does not make it particularly easy by pushing you into the virtual inheritance territory.

    You could adopt a callback approach that is common in POSIX thread libraries (passing a void pointer and a function pointer that takes a void pointer), but that is not too C++-ish.

    Finally, if you are using C++11, you have anonymous functions that let you address the issue very gracefully by wrapping both a function and an object on which it operates in a single closure without using an external library, such as boost.

    Here is a quick example of the third approach (link to ideone):

    #include <iostream>
    #include <string>
    using namespace std;
    
    class WithNotification {
    public:
        virtual void notify()=0;
    };
    
    class Robot : public virtual WithNotification {
    private:
        string name;
    public:
        Robot(const string& n) : name(n) {}
        virtual void notify() {cout << name << " has been notified" << endl; }
    };
    
    class Task {
    private:
        WithNotification& onFinished;
    public:
        Task(WithNotification& f) : onFinished(f) {}
        void run() {
            cout << "The task is running" << endl;
            onFinished.notify();
        }
    };
    
    int main() {
        Robot r1("Quick");
        Robot r2("Brown");
        Task t1(r1);
        Task t2(r2);
        t1.run();
        t2.run();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine the following code: class foreach_convert { public static void method2() { List<IComparable> x
Imagine in the Global.asax.cs file I had an instance class as a private field.
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Like this: public class remoteStatusCounts : RemoteStatus { public int statusCount; public remoteStatusCounts(RemoteStatus r)
imagine that you had a task that finished in, say 10 seconds. Now, after
Imagine I had an id column and each of its rows contained a number.
imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
Imagine an architecture composed of 2 Virtual Machines VM1 and VM2 hosted somewhere in
Imagine you have class A which has code which runs as method M. And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.