Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > can a class member function be used as a callback function?

Reply
Thread Tools

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

 
 
JDT
Guest
Posts: n/a
 
      03-28-2007
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-28-2007
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.
 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      03-28-2007
On Wed, 28 Mar 2007 01:29:42 GMT in comp.lang.c++, JDT
<(E-Mail Removed)> wrote,
>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/


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-28-2007
David Harmon wrote:
> On Wed, 28 Mar 2007 01:29:42 GMT in comp.lang.c++, JDT
> <(E-Mail Removed)> wrote,
>> 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.
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      03-28-2007
On Wed, 28 Mar 2007 11:53:17 +0300 in comp.lang.c++, Juha Nieminen
<(E-Mail Removed)> wrote,
>David Harmon wrote:
>> On Wed, 28 Mar 2007 01:29:42 GMT in comp.lang.c++, JDT
>> <(E-Mail Removed)> wrote,
>>> 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.


 
Reply With Quote
 
Christopher Pisz
Guest
Posts: n/a
 
      03-28-2007
"JDT" <(E-Mail Removed)> wrote in message
news:ayjOh.2621$(E-Mail Removed) ...
> 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.




 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-29-2007
On Mar 28, 3:29 am, JDT <(E-Mail Removed)> wrote:

> 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:(E-Mail Removed)
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Pointer-to-member-function pointing to a member function of an inherited class akiriwas@gmail.com C++ 12 02-11-2005 05:15 PM
Howto: Class member function as callback function for dialog box prettysmurfed C++ 6 07-22-2003 06:17 PM



Advertisments