473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 1273
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and...
4
by: womanontheinside | last post by:
I have a library which was written in C, you call a function, it provides the result by a callback to specific function names. I am trying to wrap the calls to this inside a class, but this causes...
21
by: Šimon Tóth | last post by:
Hi, i need to use callbacks in C++. I found great article from Rich Hickey, and i'm using his callback.h file in my project. The problem is that when I compile my project I get two pages of...
5
by: WAkthar | last post by:
Hi, can anyone show me how to be able to pass the name of method to a dll function which will call this method when something inside the dll calling function happens. Is this called a callback?...
8
by: Manuel | last post by:
I'm trying to write a class for a simple openGL GUI. I've written a method reshape: ------------------------ void MHwindow::reshape(int w, int h) { glViewport ( 0, 0, w, h ); ...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
13
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of...
3
by: SpreadTooThin | last post by:
I have a C routine that wants to call a method of its user. In this case the method is a method inside a class. The C routine is passed a void * which can be used by the user any way they like. I...
5
by: Michael Oswald | last post by:
Hello all, I'm working on a project where I came across some situations, where the GUI library works with normal C callbacks. The code has been implemented by many people, so I came across...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.