473,698 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ 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 3447
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*******@yaho o.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*******@yaho o.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*******@yah oo.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*******@yaho o.comwrote in message
news:ay******** ********@newssv r29.news.prodig y.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...@yaho o.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 objektorientier ter Datenverarbeitu ng
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
10865
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...
12
2437
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.
6
2638
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 be called within the threads context (like setting the cancelability state and type). That´s why I am using a function that initializes the thread and then calls the original thread function. The problem is, that I can't manage to implement the...
9
8253
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
4
5402
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 resolution means that expire time should be measured in microseconds, or at least in milliseconds. I had a look on Gnu C <sys/time.h>'s functions getitimer and setitimer, but these all use a signal (SIGALRM) to notify expiration, and I don't know the...
2
13262
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 valid callback in ..." I tried both myclass::myfunction and $this->myfunction, neither worked. Is there really no way of using a class member as a callback, it has to be an external function? That sucks... :(
3
2565
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
1742
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 window in the OS (I'm just trying to encapsulate everything that is particularly OS specific in my code). When the class is created the constructor is called and part of the goodness of window creation involves registering a callback that deals
3
1506
by: Vulcan | last post by:
<code> class Base { }; class Class1 : public Base{ public: void Function1(); }
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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
5862
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.