C ++成员函数作为回调 - 教程? [英] C++ member function as a callback - tutorial?

查看:146
本文介绍了C ++成员函数作为回调 - 教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道一些很好的

教程关于使用C ++成员函数作为使用函数指针的回调吗?

解决方案

IMO,成员函数指针与普通函数指针没有太大区别,只是成员函数指针与类作用域绑定。这些是一些链接。



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





这应该很容易。成员回调函数应该是静态并且应该接受至少一个参数,该参数是回调对象的指针,它是具有回调方法的类的实例。



下面的类有一个回调方法_MyCallbackMethod:



 < span class =code-keyword> class  CMyCallbackClass 
{
public
static void __ stdcall _MyCallbackMethod( void * pObject, int iMyParam>)
{
CMyCallbackClass * pThis = reinterpret_cast< cmycallbackclass *>(pObject);
pThis-> MyCallbackMethod(iMyParam);
}

void MyCallbackMethod( int iMyParam)
{
// 此处的代码
}
};



最后,你给另一个对象指向另一个类的静态方法_MyCallbackMethod:

 < span class =code-keyword> class  X 
{
public
void SetCallbackMethod( void (* methoPtr)( int ))
{
- > m_pPtr = methoPtr;
}
};



X类将按以下方式调用回调:

   - > pPtr( 10 ); 





祝你好运,

JK


在关键字委托下查看。 CodeProject上有几篇关于这个主题的好文章。委托是简单的单词成员函数指针和对象指针的组合。它允许您从一个实体调用特定对象的某个成员函数。



代表,如果实现得当,比经典更容易处理C ++成员函数指针。


Hi, does someone know some nice
tutorial about using C++ member functions as callbacks using function pointers??

解决方案

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


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.


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.


这篇关于C ++成员函数作为回调 - 教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆