Skip to main content
gamedev.net
🔒 Locked

Pointer to a Function?

Started by Rob Loach Nov 17, 2003 at 9:05 PM 2 replies 850+ views
Original Post
Rob Loach
Rob Loach
Is it possible to pass a name/pointer of a function through a method so that the method can call the function? Example:
int *foo()
{
    return 5;
}

int fooMethod(int *function())
{
    // Call the function passed through
    function();
    return 0;
}

int main()
{
    // Call the foo method passing through the foo function.
    fooMethod( foo() );
    return 0;
}
   
..... Is it possible to do that some how? I know this example seems a bit usless, but what I'm using it for would just make the program kick ass. [edited by - Rob Loach on November 17, 2003 10:06:18 PM]
Rob Loach [Website] [Projects] [
Namethatnobodyelsetook
Namethatnobodyelsetook
Yes. Changed your sample to int from int *, since you''re not returning an int *...


int foo(void)
{
return 5;
}

int fooMethod(int (* function)(void))
{
function();
return 0;
}

int main()
{
fooMethod( foo );
return 0;
}

Also click here
GreenToad
GreenToad
In C and C++ it is completely possible to do this. Here's how.

Edit: Too late...

[edited by - GreenToad on November 17, 2003 11:17:40 PM]
Rob Loach
Rob Loach
quote:
Original post by Namethatnobodyelsetook
Yes. Changed your sample to int from int *, since you''re not returning an int *... [ code ]
Also click here



Awesome! Thanks a lot!

Hmmm, they make it WAY more complicated then it actually is.
Rob Loach [Website] [Projects] [

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.