callbacks to member functions

F

Fred Baxter

It seems a common way for doing a callback from one class to another is to
do something like this, is there a better way?

struct A
{
static void call_me_back( A* instance, int new_i )
{
instance->i = new_i;
}
int i;
};

struct B
{
void set_registered_fn( A* a, void (*fn)( A*, int ) )
{
registered_instance = a;
registered_fn = fn;
}
void do_callback(){ registered_fn( registered_instance, 0 ); }

void (*registered_fn)( A*, int );
A* registered_instance;
};

void f()
{
A a;
B b;
b.set_registered_fn( &a, A::call_me_back );
b.do_callback();
}
 
V

Victor Bazarov

Fred said:
It seems a common way for doing a callback from one class to another is to
do something like this, is there a better way?

struct A
{
static void call_me_back( A* instance, int new_i )
{
instance->i = new_i;
}
int i;
};

struct B
{
void set_registered_fn( A* a, void (*fn)( A*, int ) )
{
registered_instance = a;
registered_fn = fn;
}
void do_callback(){ registered_fn( registered_instance, 0 ); }

void (*registered_fn)( A*, int );
A* registered_instance;
};

void f()
{
A a;
B b;
b.set_registered_fn( &a, A::call_me_back );
b.do_callback();
}

It is possible (and not very difficult) to rewrite your code snippet to
use pointers to non-static members of 'A'. Essentially the same result
is achieved.

What don't you like about this way that makes you to look for "better"?

Victor
 
F

Fred Baxter

Victor Bazarov said:
Fred Baxter wrote:
It is possible (and not very difficult) to rewrite your code snippet to
use pointers to non-static members of 'A'. Essentially the same result
is achieved.

What don't you like about this way that makes you to look for "better"?

It just seems a bit inelegant to be passing pointers to isntances to be used
in static functions. How would you re-write the code to use pointer to
non-static member? That would seem more robust, if it encapsulated the
registered class instance as well as its member function.
 
V

Victor Bazarov

Fred Baxter said:
It just seems a bit inelegant to be passing pointers to isntances to be
used in static functions. How would you re-write the code to use pointer
to non-static member? That would seem more robust, if it encapsulated the
registered class instance as well as its member function.

A pointer to member will *not* eliminate the necessity to pass a pointer or
a reference to the instance. Of course, if you use a reference you need to
initialise it and you'll never get the second chance, with a pointer you
can change it later on.

class CallMeBack {
public:
void foo(int);
void bar(int);
};

class WillCallBack {
void (CallMeBack::*cb)(int);
CallMeBack *pcb;
public:
WillCallBack(CallMeBack *p, void (CallMeBack::*f)(int))
: pcb(p), cb(f) {}

void doit(int i) { (ocb->*cb)(i); }

void change_cb(void (CallMeBack::*f)(int)) { cb = f; }
void change_ptr(CallMeBack *p) { pcb = p; }
};

int main()
{
CallMeBack waiting;
WillCallBack promise(&waiting, &CallMeBack::foo);
promise.doit(42);
promise.change_ptr(&CallMeBack::bar);
promise.doit(42);
CallMeBack another;
promise.change_ptr(&another);
promise.doit(42);
}

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top