tags:

views:

74

answers:

2

Hi,

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?

+4  A: 

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 (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]

David
+1  A: 

The C++ standard, in its section on Unary operators, states that a pointer-to-member requires explicit use of &.

Troubadour