gpt4 book ai didi

c++ - 使用类成员函数作为回调

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:25:55 28 4
gpt4 key购买 nike

在 PortAudio 的 C++ 绑定(bind)中,有一个 MemFunCallBackStream 构造函数,可以调用为:

portaudio::MemFunCallbackStream<MyClass> streamRecord(paramsRecord, 
*AnInstanceOfMyClass,
&MyClass::MemberFunction);

最后一个参数是回调函数。但是,如果不在该参数上使用 & 运算符,编译器将失败。但据我所知,在获取要在函数指针中使用的函数地址时, & 参数是可以省略的。这与 C 回调函数和 ptr 有什么不同吗?发挥作用。语法?

最佳答案

This FAQ似乎建议您可以省略&(至少对于静态成员函数),但随后又给出了各种不应该混淆普通函数的理由-带有 C++ 成员函数指针的指针。

编辑:找到更多信息here ,与非静态成员函数相关:

Some compilers (most notably MSVC 6 and 7) will let you omit the &, even though it is non-standard and confusing. More standard-compliant compilers (e.g., GNU G++ and MSVC 8 (a.k.a. VS 2005)) require it, so you should definitely put it in. To invoke the member function pointer, you need to provide an instance of SomeClass, and you must use the special operator ->*. This operator has a low precedence, so you need to put it in parentheses. [Emphasis added]

关于c++ - 使用类成员函数作为回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3475920/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com