• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++: Passing a function to a function

TheLonelyPhoenix

Diamond Member
I'm trying to build a class which allows a member function to take another function as a parameter. However, I can't seem to find a good example of how this is done. I particular I need to know how to structure the function definition for this.

I hope that made sense. Any ideas?
 
In C, you use function pointers to refer to anonymous functions. Then you'd declare your own function that accepts a "pointer to function" as one of its parameters. The caller would pass a pointer to the desired function, and whoila... you have just passed a function as a parameter.



In C++ you can use an object that wraps a function pointer, called a "functor". You can read more about those at this Function Pointer site.
 
BTW, if you're trying to pass a C++ member function, then the syntax is slightly more complicated. You need what is called a "pointer to a member."

int (ClassName::*pMember)( /* parameter types here */ );

See Stroustrup 3rd ed. section 15.5, "Pointers to members."
 
Back
Top