472,782 Members | 1,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 1268
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.