3

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?

2

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

1 Comment

Also take a look into boost::function, boost::bind for other solutions to the same problem.
2

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).

Comments

2

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

1 Comment

It's worth noting for those following along at home that it's not so important what type is BigClass::PassThis so long as the result of applying the & operator to it gets you a pointer to the appropriately typed function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.