473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2684
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.ho tmail.com> wrote in message news:<sC******* ********@newsfe 3-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
10868
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 can't seem to get the adress of the member function stated properly in the DialogBox function. I thought it was enough to write it as Dialog::DlgProc, if it was a normal situation and DlgProc was a normal function (not member of anything) then you...
1
665
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 member function?
12
2443
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 variables, but don't know how to do it ? I still receive errors when I try to use it.
9
8256
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 solution? Declaring the function extern "C" fails, because linkage declarations must be made at file scope. #include <stdlib.h> //for qsort template <class T> class Sorter
3
3062
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 shared_ptr<cBaseas a parameter, and return void. The code was working fine, although I have encountered problems (under a Microsoft compiler, of course - VC 8.0) when I attempt to add callbacks to a class with multiple inheritance. I hate multiple
1
7588
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
3451
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
7683
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 a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which need to be implemented by the C++ wrapper. These callback functions are declared as "extern C...
5
3122
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 than making the func static? For ex: class foo { some_type x void fa();
5
4663
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
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.