Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi, does someone know some nice
tutorial about using C++ member functions as callbacks using function pointers??
Posted 21-Feb-13 0:36am
Edited 21-Feb-13 0:37am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

IMO, member function pointer is not very different from ordinary function pointer except the member function pointer is bound with the class scope. These are some of the links.
 
http://support.microsoft.com/kb/94579
 
http://www.tutok.sk/fastgl/callback.html
 
http://blogs.msdn.com/b/oldnewthing/archive/2004/02/09/70002.aspx
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
this shall be quite easy. Member callback function shall be static and should accept at least one parameter that is the pointer the callback object, which is an instance of the class that has the callback method.
 
The following class has a callback method _MyCallbackMethod:
 
class CMyCallbackClass
{
public:
   static void __stdcall _MyCallbackMethod(void* pObject, int iMyParam>)
   {
       CMyCallbackClass* pThis = reinterpret_cast<cmycallbackclass*>(pObject);
       pThis->MyCallbackMethod(iMyParam);
   }   
   
   void MyCallbackMethod(int iMyParam)
   {
       // your code here
   }
};
And finally, you give another object a pointer to the static method _MyCallbackMethod to another class:
class X
{
public:
   void SetCallbackMethod(void (*methoPtr)(int))
   {
      this->m_pPtr = methoPtr;
   }
};
The X class will call the callback in the following way:
   this->pPtr(10);

 
Best regards,
J. K.
  Permalink  
Comments
H.Brydon at 21-Feb-13 12:01pm
   
Good answer - +5
 
[Important part is that it needs to be static since you can control scope but can't get 'this' context.]
Matra Divat at 21-Feb-13 17:17pm
   
Hi, thanks for your response. But should it not be:
void SetCallbackMethod(void (*methoPtr)(void*)(int)) instead of:
void SetCallbackMethod(void (*methoPtr)(int))??
 
and ps. one more question, how would you define m_pPtr in class x? e.g., its type etc.?? Thanks.
Matra Divat at 22-Feb-13 10:05am
   
pps. so it's not possible to avoid wrapper functions with member function pointers right?? in case if you want to use a pointer to generic class member function ...??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Look under the keyword "Delegate". There are several nice articles about this subject here on CodeProject. A delegate is in simple words the combination of a member function pointer and an object pointer. It allows you to call a certain member function of a specific object from just a single entity.
 
Delegates, when implemented right, are much easier to deal with than the classical C++ member function pointers.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 604
1 thatraja 370
2 OriginalGriff 329
3 Sergey Alexandrovich Kryukov 290
4 CPallini 219
0 OriginalGriff 697
1 Dnyaneshwar@Pune 604
2 Kornfeld Eliyahu Peter 495
3 thatraja 370
4 Sergey Alexandrovich Kryukov 344


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100