473,405 Members | 2,272 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,405 software developers and data experts.

Member Function Callback

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
Jul 22 '05 #1
2 2662
Chris Morley wrote:
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
Jul 22 '05 #2
"Chris Morley" <g1**@nospam.hotmail.com> wrote in message news:<sC***************@newsfe3-win.ntli.net>...
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: prettysmurfed | last post by:
Hi all I have a bit of a problem, the subject of this post is almost selfexplaing. But here goes: Heres an example of the code I want to implement, its all nice and simple, but the flaw is I...
1
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
12
by: MacFly | last post by:
Hi everyone, HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer) I want that method to be class member method so it could have access to class...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
3
by: ryan.mitchley | last post by:
Hi all I have a class (cPort) that is designed to receive objects and, depending on the type, call a handler (callback) in any descendant of a cProcessBlock class. Callback functions take a...
1
by: pheres | last post by:
Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Code: void callOut( void (*callback)() ) { callback();
6
by: JDT | last post by:
Hi, Can we pass a member function in a class as a callback function? Someone instucted me that I can only use a static functon or a global function as a callback. Your help is appreciated. JD
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
5
by: Neo | last post by:
hi, I searched in the groups here but didnt find any post answering my question. My question is I need the address of the member function in the same class. Is there anyway to go about it other...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.