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 974313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:25:01+00:00 2026-05-16T03:25:01+00:00

I want to implement a class in c++ that has a callback. So I

  • 0

I want to implement a class in c++ that has a callback.

So I think I need a method that has 2 arguments:

  • the target object. (let’s say
    *myObj)
  • the pointer to a member function of
    the target object. (so i can do
    *myObj->memberFunc(); )

The conditions are:

  • myObj can be from any class.

  • the member function that is gonna be the callback function is non-static.

I’ve been reading about this but it seems like I need to know the class of myObj before hand. But I am not sure how to do it. How can I handle this? Is this possible in C++?

This is something I have in mind but is surely incorrect.

class MyClassWithCallback{
public
    void *targetObj;
    void (*callback)(int number);
    void setCallback(void *myObj, void(*callbackPtr)(int number)){
        targetObj = myObj;
        callback = callbackPtr;
    };
    void callCallback(int a){
        (myObj)->ptr(a);
    };
};
class Target{
public
    int res;
    void doSomething(int a){//so something here. This is gonna be the callback function};        
};

int main(){
    Target myTarget;
    MyClassWithCallback myCaller;
    myCaller.setCallback((void *)&myTarget, &doSomething);

}

I appreciate any help.

Thank you.

UPDATE
Most of you said Observing and Delegation, well that’s i exactly what i am looking for, I am kind of a Objective-C/Cocoa minded guy.
My current implementation is using interfaces with virtual functions. Is just I thought it would be “smarter” to just pass the object and a member function pointer (like boost!) instead of defining an Interface. But It seems that everybody agrees that Interfaces are the easiest way right? Boost seems to be a good idea, (assuming is installed)

  • 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-05-16T03:25:02+00:00Added an answer on May 16, 2026 at 3:25 am

    The best solution, use boost::function with boost::bind, or if your compiler supports tr1/c++0x use std::tr1::function and std::tr1::bind.

    So it becomes as simple as:

    boost::function<void()> callback;
    Target myTarget;
    callback=boost::bind(&Target::doSomething,&myTarget);
    
    callback(); // calls the function
    

    And your set callback becomes:

    class MyClassWithCallback{
    public:
      void setCallback(boost::function<void()> const &cb)
      {
         callback_ = cb;
      }
      void call_it() { callback_(); }
    private:
      boost::function<void()> callback_;
    };
    

    Otherwise you need to implement some abstract class

    struct callback { 
     virtual void call() = 0;
     virtual ~callback() {}
    };
    
    struct TargetCallback {
     virtual void call() { ((*self).*member)()); }
     void (Target::*member)();
     Target *self;
     TargetCallback(void (Target::*m)(),Target *p) : 
           member(m),
           self(p)
     {}
    };
    

    And then use:

    myCaller.setCallback(new TargetCallback(&Target::doSomething,&myTarget));
    

    When your class get modified into:

    class MyClassWithCallback{
    public:
      void setCallback(callback *cb)
      {
         callback_.reset(cb);
      }
      void call_it() { callback_->call(); }
    private:
      std::auto_ptr<callback> callback_;
    };
    

    And of course if the function you want to call does not change you may just implement some interface, i.e. derive Target from some abstract class with this call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a method that will find stuff in my custom class.
So let's say I have two classes that inherit a base class that has
How can I implement a class/object that has a reference to a bunch of
I have a base class that has an abstract getType() method. I want subclasses
I want to implement a derived class that should also implement an interface, that
I have a class that instantiates two classes which implement interfaces. I want one
I want to implement a generic method on a generic class which would allow
I want to implement my own copy method for a class A which looks
I want to implement a class Address that, when created, initializes its field addr
I have a class in .NET that implements IXmlSerializable. I want to serialize its

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.