Tutorial :Function pointer problem



Question:

I am trying to use a function pointer, but the 3 lines below just do not seem to want to cooperate...

I'm getting error code C3867.

Can you see what I'm doing wrong?

In .h file

void MyFunc(int, FILEINFO*(*)(FILEINFO*), FILEINFO*, int);  

The definition in the .cpp file

void MyFunc(int number, FILEINFO*(*GetFiles)(FILEINFO*), FILEINFO* args, int type);  

Then here is where I'm actually calling the function

MyFuncClass->MyFunc(GetNumber(), &BigClass::PassThis, GetArgs(), TheType);  

Any problems jump out?


Solution:1

You cannot pass a non-static member function of a class as an ordinary function pointer, since a member function implicitly uses the this-pointer. A solution for this is to define a static member function that takes a pointer to the class as it's first argument and wraps the call to BigClass::PassThis and pass a pointer to that member function instead. Please see The Function Pointer Tutorials for more information.

A better solution might be to look into using functors instead.


Solution:2

What is the definition of BigClass::PassThis()? Is it a static class member function or a regular member function? If it's a regular member function, you can't do that, because it has a hidden this parameter. See question 33.4 of the C++ FAQ Lite.


Solution:3

We'd need to see the definition of BigClass. Unless PassThis is a static member function, what you've done can't work because regular member functions can't be passed as a function pointer the way you are doing it (among other things, you've got no 'this' parameter).


Solution:4

I'm guessing that &BigClass::PassThis is a pointer to a member function and not a pointer to an ordinary function.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »