简体   繁体   中英

Using class member function as Callback

In PortAudio's C++ bindings, there is a MemFunCallBackStream constructior that can be called as:

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

where last parameter is the callback function. However without using the & operator on that parameter compiler fails. But as far as I know & parameter is omitable when obtaining address of functions to use in function pointers. Is this somehow different from C callback function and ptr. to func. syntax?

This FAQ seems to suggest that you can omit the & (for static member functions, at least), but then goes on to give various reasons why you shouldn't confuse ordinary function-pointers with C++ member-function-pointers.

EDIT : Found more information here , which is relevant to non-static member functions:

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 (eg, GNU G++ and MSVC 8 (aka 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 ++标准在其关于一元运算符的部分中指出,指向成员的指针需要显式使用&。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM