By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

callbacks to member functions

P: n/a
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();
}
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Fred Baxter wrote:
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
Jul 23 '05 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:WK*******************@newsread1.mlpsca01.us.t o.verio.net...
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.
Jul 23 '05 #3

P: n/a
"Fred Baxter" <df*****@fieieied.com> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:WK*******************@newsread1.mlpsca01.us.t o.verio.net...
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.


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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.