Member Function Callback

C

Chris Morley

Hi, I have always done my C++ class callbacks with the age old 'using this
pointer in parameter of the class's static callback function' and
typecasting it to get the specific instance.

However I'm left wondering are there any better ways of doing this?

Just before I left work today I came across functors, which look promising.
I will have a proper look tomorrow, but I have a few questions in the mean
time.

Is it possible to use functor classes with standard, precompiled, c style
callback code, passing them in as pointers? Or do I need to pass a pointer
to the base class of the translating class and have to recompile code in
order for the derefencing to work?

I'm going to have a look online now for some examples whereby callbacks are
made compile time safe, in the meantime (and indeed if I cant find any), can
anyone recommend some good examples?

Many thanks in advance,

Chris
 
V

Victor Bazarov

Chris said:
Hi, I have always done my C++ class callbacks with the age old 'using this
pointer in parameter of the class's static callback function' and
typecasting it to get the specific instance.

However I'm left wondering are there any better ways of doing this?

Just before I left work today I came across functors, which look promising.
I will have a proper look tomorrow, but I have a few questions in the mean
time.

Is it possible to use functor classes with standard, precompiled, c style
callback code, passing them in as pointers?

Usually no. Functors work "like functions" in template code. That's
what they are good for.
Or do I need to pass a pointer
to the base class of the translating class and have to recompile code in
order for the derefencing to work?

Not sure what you mean here, but probably yes.
I'm going to have a look online now for some examples whereby callbacks are
made compile time safe, in the meantime (and indeed if I cant find any), can
anyone recommend some good examples?

"Callbacks" are the way to do things using standard library's "for_each"
and similar algorithms. A good book on Standard Library should have many
examples you're looking for.

Victor
 
U

Uwe Schnitker

Chris Morley said:
Hi, I have always done my C++ class callbacks with the age old 'using this
pointer in parameter of the class's static callback function' and
typecasting it to get the specific instance.

However I'm left wondering are there any better ways of doing this?

Just before I left work today I came across functors, which look promising.
I will have a proper look tomorrow, but I have a few questions in the mean
time.

Is it possible to use functor classes with standard, precompiled, c style
callback code, passing them in as pointers?
No.

Or do I need to pass a pointer
to the base class of the translating class and have to recompile code in
order for the derefencing to work?

No.

You can write a wrapper function that calls your functor. If your C style
callback uses a void pointer argument, the wrapper function can cast it
into the functor type and call that functor. Using some simple template
tricks, all fo this can be done automatically for different functors.

For an example how this can be done you could look at the FLTK library.
This GUI toolkit uses C style callbacks with void pointers. An extension
library "fl_connect" can be used to create wrapper functions and functors
without any change to the existing library.
I'm going to have a look online now for some examples whereby callbacks are
made compile time safe, in the meantime (and indeed if I cant find any), can
anyone recommend some good examples?

Check out Steve Dewhursts "Function pointers and function objects", available
on his webside
www.semantics.org
in the Presentations section.
Many thanks in advance,

Chris

Uwe
 

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
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top