如何传递非静态成员函数作为回调?

编程入门 行业动态 更新时间:2024-10-28 12:23:54
本文介绍了如何传递非静态成员函数作为回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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).

更多推荐

如何传递非静态成员函数作为回调?

本文发布于:2023-11-06 04:30:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   静态   函数   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!