473,466 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

can a class member function be used as a callback function?

JDT
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
Mar 28 '07 #1
6 3428
JDT wrote:
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.
Yes, you can. But you have to pass an instance of the class as well in
order to call the function.

--
Ian Collins.
Mar 28 '07 #2
On Wed, 28 Mar 2007 01:29:42 GMT in comp.lang.c++, JDT
<jd*******@yahoo.comwrote,
>Can we pass a member function in a class as a callback function?
In general, no.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"# [33.2] How do I pass a pointer-to-member-function to a signal
handler, X event callback, system call that starts a thread/task, etc?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/
Mar 28 '07 #3
David Harmon wrote:
On Wed, 28 Mar 2007 01:29:42 GMT in comp.lang.c++, JDT
<jd*******@yahoo.comwrote,
>Can we pass a member function in a class as a callback function?

In general, no.
What do you mean "in general"? I think that's confusing.

You can, of course, get a pointer to a member function and pass
it around all you like. It is perfectly possible to construct a
system where a member function is called as a callback.

Of course the catch is that this function pointer alone is not
enough. Member functions implicitly take a pointer to an instance
of the class (that's the 'this' pointer seen inside the function)
and thus to call the function such an instance (well, a pointer to
one) is needed. The function cannot be called alone. If you want
the callback system to call a member function of a specific instance
of the class, you have to give it a pointer to that instance too,
besides the pointer to the member function.

Naturally if the callback system only takes a function pointer
and nothing else, and you can't modify the callback system, then
you are out of luck.
Mar 28 '07 #4
On Wed, 28 Mar 2007 11:53:17 +0300 in comp.lang.c++, Juha Nieminen
<no****@thanks.invalidwrote,
>David Harmon wrote:
>On Wed, 28 Mar 2007 01:29:42 GMT in comp.lang.c++, JDT
<jd*******@yahoo.comwrote,
>>Can we pass a member function in a class as a callback function?

In general, no.

What do you mean "in general"? I think that's confusing.
It means, don't bother nitpicking.
Mar 28 '07 #5
"JDT" <jd*******@yahoo.comwrote in message
news:ay****************@newssvr29.news.prodigy.net ...
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

I've has success using static methods of a class as callback also. In on I
implemented myself, the callback took an instance as an argument, then the
one static callback could dispatch to the particular instance. In some
cases, maybe you don't care about the instance and just want one callback,
but then should it really be a member of a class? so, to answer your
question, it is possible, but how it is possible really depends on how the
callback mechanism was designed.


Mar 28 '07 #6
On Mar 28, 3:29 am, JDT <jdt_yo...@yahoo.comwrote:
Can we pass a member function in a class as a callback function?
You can pass whatever the interface requires, and nothing else.
I've even seen some interfaces (in a Windows manager for Sun)
which used pointers to member function for the callbacks. It's
rather exceptional, however. Typically, two cases occur:

-- The interface was designed with C++ in mind. In that case,
most of the time, you pass pointers to functional objects as
the callback. Traditionally, those objects must derive from
a common base class, and override a specific function in
that class, but some newer interfaces use more or less fancy
template tricks to allow you to pass pretty much anything
which will support a function call operator.

-- The interface is designed for C or C/C++. In that case,
you'll almost certainly have to pass the address of an
`extern "C"' function: static members don't cut it, nor do
normal global functions in C++. Hopefully, you'll also get
the chance to pass a void* with the address of "user data";
your user data will be a functional object, as above, which
gets called in the function whose address you pass.
Someone instucted me that I can only use a static functon or a global
function as a callback.
To date, I've never seen an interface where a static member
function could be used (except perhaps some of those using fancy
templates). See above: it very much depends on the interface,
and it's possible for an interface to use just about anything.
What you do have to do is conform to the interface: if it
expects a pointer to a member function of class X, you have to
pass it a pointer to a (non-static) member fucntion of class X.
If it has a C compatible interface, you must pass it a function
declared `extern "C"'. And if it expects a pointer to a class
CallBack, you have to derive from CallBack, and pass it a
pointer to an object of the derived type.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 29 '07 #7

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...
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...
6
by: Christian Buckl | last post by:
Hi, I try to implement my own thread class based on POSIX threads. I want my class to manage everything (creation of threads, exception handling...). This includes also some functions that need to...
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...
4
by: Dmitri Sologoubenko | last post by:
Hi, guys! I need a C++ class (Linux/POSIX/GNU C++), which can be started and stopped, and calls a virtual callback instance method (e.g. "expired()") when a given time has elapsed. High...
2
by: Kimmo Laine | last post by:
I tried to use a class member function as a callback in preg_replace_callback, but failed. It announces: " Warning: preg_replace_callback() : requires argument 2, 'myclass::myfunction', to be a...
3
by: tom | last post by:
How to get access to veriable from static class member? class cTest { public: int a; void Set_a(){a = 1;} static int dlgTest()
2
by: tendots | last post by:
Hi all, I have come up against a problem when I am registering a callback within a constructor. The code that I am writing contains a class that deals with everything to do with bringing up a...
3
by: Vulcan | last post by:
<code> class Base { }; class Class1 : public Base{ public: void Function1(); }
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:
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
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...
1
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.