Tuesday, July 28, 2009

How do you pass a function to another function in C++?

Hi, I was wondering how I could pass one function as a parameter to another function, code below...

How do you pass a function to another function in C++?
You code the original function like this


void Caller(void (*fn)())


{


fn(); %26lt;- This calls the passed in function.


(*fn)(); %26lt;- This is what you used to have to do to call a passed in function.


}





Call it like this


void MyCall() - Must match the definition


Caller(MyCall)
Reply:using function pointers here is a link to a pretty good tutorial





http://www.newty.de/fpt/index.html


No comments:

Post a Comment