如何传递非静态成员函数作为回调? [英] how to pass a non static-member function as a callback?

查看:416
本文介绍了如何传递非静态成员函数作为回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 
io_iterator_t enumerator;
kern_return_t   result;
result = IOServiceAddMatchingNotification(
             mNotifyPort,
             kIOMatchedNotification,
             IOServiceMatching( "IOFireWireLocalNode" ),
             serviceMatchingCallback, 
             (void *)0x1234,
             & enumerator );



serviceMatchingCallback((void *)0x1234, enumerator);

如果我声明serviceMatchinCallback作为静态,那么它工作,但我不想它静态。有没有办法传递它一个非静态的回调函数?

if i declare serviceMatchinCallback as static then it works, but i do not want it to be static. Is there a way to pass it a non-static callback function?

谢谢

推荐答案

你可以保持它的静态,但使用userdata存储这个指针,除了你想要的其他用户数据(通过打包成一个结构,例如),然后通过调用 this-> someCallback (其中 this 是存储在userdata中的指针,当然)。

You could keep it static, but use the userdata to store the this pointer in addition to whatever other userdata you want (by packing them into a structure, for example) and then call an object-specific callback from the static version by calling this->someCallback (where this is the pointer stored in the userdata, of course).

这篇关于如何传递非静态成员函数作为回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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