473,395 Members | 1,616 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,395 software developers and data experts.

Need help with pointers to class methods being used as callbacks...

I have a C routine that wants to call a method of its user.
In this case the method is a method inside a class.
The C routine is passed a void * which can be used by the user any way
they like.
I use that parameter and pass the actual instance of the C++ class
that performed the call.
The C callback handler then uses the instance of the class and calls
the appropriate method.

Here is the code but it doesn't work. What am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. void DeviceFoundCallBack(void * userRefCon,
  2. IOBluetoothDeviceInquiryRef inquiryRef, IOBluetoothDeviceRef
  3. deviceRef)
  4. {
  5. userRefCon->DeviceFoundCallBack(inquiryRef, deviceRef);
  6. }
  7.  
  8. void CompleteCallback(void * userRefCon, IOBluetoothDeviceInquiryRef
  9. inquiryRef, IOBluetoothDeviceRef deviceRef)
  10. {
  11. userRefCon->CompleteCallBack(inquiryRef, deviceRef);
  12. }
  13.  
  14. void BlueTooth::startSearch(void)
  15. {
  16. _inquiry =
  17. IOBluetoothDeviceInquiryCreateWithCallbackRefCon(this);  // This will
  18. be passed back to us to let us know the instance.
  19. IOBluetoothDeviceInquirySetDeviceFoundCallback(_inquiry,
  20. &DeviceFoundCallBack);
  21. }
  22.  
Aug 30 '07 #1
3 2048
SpreadTooThin wrote:
I have a C routine that wants to call a method of its user.
In this case the method is a method inside a class.
The C routine is passed a void * which can be used by the user any way
they like.
I use that parameter and pass the actual instance of the C++ class
that performed the call.
The C callback handler then uses the instance of the class and calls
the appropriate method.

Here is the code but it doesn't work. What am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. void DeviceFoundCallBack(void * userRefCon,
  2. IOBluetoothDeviceInquiryRef inquiryRef, IOBluetoothDeviceRef
  3. deviceRef)
  4. {
  5. userRefCon->DeviceFoundCallBack(inquiryRef, deviceRef);
Expand|Select|Wrap|Line Numbers
  1.  
  2. 'userRefCon' has to be of a class type or pointer to a class to use
  3. the -syntax.  You have a void*, which is not a class.  You need to
  4. convert your 'void*' into a pointer to the class:
  5.  
  6. someclasstype* pObj = static_cast<someclasstype*>(userRefCon);
  7.  
  8. only then you will be alble to call your member function.
  9.  
  10.         
  11.                 }
  12. void CompleteCallback(void * userRefCon, IOBluetoothDeviceInquiryRef
  13. inquiryRef, IOBluetoothDeviceRef deviceRef)
  14. {
  15. userRefCon->CompleteCallBack(inquiryRef, deviceRef);
  16. }
  17. void BlueTooth::startSearch(void)
  18. {
  19. _inquiry =
  20. IOBluetoothDeviceInquiryCreateWithCallbackRefCon(this);  // This will
  21. be passed back to us to let us know the instance.
  22. IOBluetoothDeviceInquirySetDeviceFoundCallback(_inquiry,
  23. &DeviceFoundCallBack);
  24.  
  25. You may have problems with the second argument here.  See the FAQ about
  26. the callbacks and member functions.
  27.  
  28.         
  29.                 }
  30.  
  31.  
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 30 '07 #2
SpreadTooThin <bj********@gmail.comwrote in
news:11*********************@x40g2000prg.googlegro ups.com:
I have a C routine that wants to call a method of its user.
Uh.. then it's not really a C routine is it? C routines would have no
concept of "method"s. (It may be a C++ routine with C linkage....)
In this case the method is a method inside a class.
The C routine is passed a void * which can be used by the user any way
they like.
I use that parameter and pass the actual instance of the C++ class
that performed the call.
The C callback handler then uses the instance of the class and calls
the appropriate method.

Here is the code but it doesn't work. What am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. void DeviceFoundCallBack(void * userRefCon,
  2. IOBluetoothDeviceInquiryRef inquiryRef, IOBluetoothDeviceRef
  3. deviceRef)
  4. {
  5.      userRefCon->DeviceFoundCallBack(inquiryRef, deviceRef);
  6. }
Expand|Select|Wrap|Line Numbers
  1.  
  2. userRefCon is a void*.  voids don't have methods.  You need to cast that
  3. pointer back to the appropriate type first.
  4.  
  5.         
  6.                 >
  7. void CompleteCallback(void * userRefCon, IOBluetoothDeviceInquiryRef
  8. inquiryRef, IOBluetoothDeviceRef deviceRef)
  9. {
  10.      userRefCon->CompleteCallBack(inquiryRef, deviceRef);
  11. }
  12.  
  13. Same as above.
  14.  
  15.         
  16.                 void BlueTooth::startSearch(void)
  17. {
  18.      _inquiry =
  19. IOBluetoothDeviceInquiryCreateWithCallbackRefCon(this);  // This will
  20. be passed back to us to let us know the instance.
  21.      IOBluetoothDeviceInquirySetDeviceFoundCallback(_inquiry,
  22. &DeviceFoundCallBack);
  23. }
  24.  
  25.  
Aug 30 '07 #3
SpreadTooThin wrote:
I have a C routine that wants to call a method of its user.
In this case the method is a method inside a class.
The C routine is passed a void * which can be used by the user any way
they like.
I use that parameter and pass the actual instance of the C++ class
that performed the call.
The C callback handler then uses the instance of the class and calls
the appropriate method.

Here is the code but it doesn't work. What am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. void DeviceFoundCallBack(void * userRefCon,
  2. IOBluetoothDeviceInquiryRef inquiryRef, IOBluetoothDeviceRef
  3. deviceRef)
  4. {
  5. userRefCon->DeviceFoundCallBack(inquiryRef, deviceRef);
Expand|Select|Wrap|Line Numbers
  1.  
  2. userRefCon is a void* and does not have a member called
  3. DeviceFoundCallBack.
  4. You must use a cast in this situation to tell the compiler what type of
  5. object userRefCon actually points to.
  6.  
  7. static_cast<BlueTotth>(userRefCon)->DeviceFoundCallBack(inquiryRef,
  8. deviceRef);
  9.  
  10.         
  11.                 }
  12. void CompleteCallback(void * userRefCon, IOBluetoothDeviceInquiryRef
  13. inquiryRef, IOBluetoothDeviceRef deviceRef)
  14. {
  15. userRefCon->CompleteCallBack(inquiryRef, deviceRef);
  16.  
  17. Same problem here.
  18.  
  19.         
  20.                 }
  21. void BlueTooth::startSearch(void)
  22. {
  23. _inquiry =
  24. IOBluetoothDeviceInquiryCreateWithCallbackRefCon(this);  // This will
  25. be passed back to us to let us know the instance.
  26. IOBluetoothDeviceInquirySetDeviceFoundCallback(_inquiry,
  27. &DeviceFoundCallBack);
  28. }
  29.  
  30.  
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Aug 30 '07 #4

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
4
by: Brian | last post by:
Hi all, I am implementing an object that is currently using function pointers for callbacks to let me do something on assignment: template <class T> class MyClass{ private: typedef T& (*...
8
by: He Shiming | last post by:
Hi, I've developed a class that implements an interface definition. It looks like this: class IRecord { public: // define interface methods by pure virtual methods // no member variables }
12
by: Ken | last post by:
I am familiar with C and Java, I would like to use a style that I have become accustomed to in Java with C++ but each time I do this I have name conflict. In the past I have just worked around it...
6
by: John Rivers | last post by:
hi, here is how to do it and restore sanity to aspx html rendering: (please only reply with sensible architectural discussion - juan) put this at the end of an aspx file (or use an include at...
4
by: Jarod_24 | last post by:
What is the point with Delegates in VB.Net What can these things do that we can not allready do with the use of Interfaces, Events and Event handlers and so on... I'd like a discussion on this,...
13
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of...
1
by: John Bode | last post by:
I need a way to fake reflection in C++ code that makes as few assumptions about the data types involved as possible. I suspect there is no good answer for what I need to do, but I'll present the...
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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,...

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.