468,512 Members | 1,660 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

callbacks to member functions

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
3 1214
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

"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
"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.

Similar topics

5 posts views Thread by Christopher Jastram | last post: by
4 posts views Thread by womanontheinside | last post: by
21 posts views Thread by Šimon Tóth | last post: by
5 posts views Thread by WAkthar | last post: by
8 posts views Thread by Manuel | last post: by
9 posts views Thread by zholthran | last post: by
5 posts views Thread by Michael Oswald | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.