c++ – & : illegal operation on bound member function expression

c++ – & : illegal operation on bound member function expression

Assuming cbVideoPrerenderer is a member function in the second example, you need to say &Foo::cbVideoPrerenderer where Foo is the class it is a member of.

But that will only be valid if it is a static member function. Non-static member functions are not like normal functions, and when you form a pointer-to-member-function with the &Foo::bar syntax the thing you get back cannot be converted to a void* (it is typically something twice as large as a pointer, as it contains information about the object type).

What youre trying to do is conditionally supported behavior in C++11,
and illegal in earlier versions, in both cases. You cant reliably
convert a pointer to a function (member or otherwise) to a void*.
(Ive worked on systems where a pointer to a function was 32 bits, but a
void* only 16.)

In practice, most compilers will (illegally, in pre-C++11) ignore the
error for non-member functions. Posix requires that function
pointers and data pointers be compatible, and they are under Windows as
well. (Today: only of the systems where they werent for me was an
early Unix.) As for pointers to members: a pointer to a static member
has a type compatible to a pointer to a function (and so will work in
practice, if the compiler allows it), but a pointer to a non-static
member has a completely different type, usually with a different size,
and a different representation. About the only way you can reliably
output one is as a series of byte values: put the address in a properly
typed variable, take the address of that variable, convert it to
unsigned char const*, then use %02x to output each byte.

But the real question is why you want to do this. There is nothing that
you can reliably do with the value you output, regardlessly of how you
output it.

c++ – & : illegal operation on bound member function expression

Leave a Reply

Your email address will not be published.