-
Function pointers???
Hello everyone. I am having some trouble using function pointers in C/C++. I am building an AI engine for my game, and what I am trying to do is this: lets say I have a function called process. It would take an integer as one argument. The function would then compare it to 0. Then, if it is greater, it would call another function called die(); But what I want to be able to do is pass in any function as an argument(instead of die(); I wanted to pass in live() https://www.sharkyforums.com/images/.../2005/06/6.gif, to Process, and have Process call the function. I know this must somehow be possible with function pointers, but I never really dealt with them much before, and I'm a bit shaky. Any help would be appreciated. Thanks, SHEEP
-
I guess this doesn't work huh https://www.sharkyforums.com/images/.../2005/06/5.gif
Code:
void process(int num, function &fnc) {
}
------------------
I miss Off Topic. :(
-
A function pointer is declared like this:
Where int is the return type of the function, and fname is whatever you want the function to be called by. I believe you also have to put the parameters of the function inside the (), but I'm not sure. (I haven't ever actually used function pointers https://www.sharkyforums.com/images/.../2005/06/5.gif)
I think you can just use the function pointer just like any other function when calling it, so:
should work with the previous declaration. I think is because all functions really are just pointers to some instruction in memory, so a function pointer would be identical to a normal function.
Oh, and that would mean you'd do this to pass a function address as an argument:
Code:
int anotherfunction()
{
...
}
.
.
.
somefunction(anotherfunction);
.
.
.
int somefunction(int (*fpointer)())
{
...
}
-
Ok...let me show you exactly what I mean, because what you showed me Strogian didnt work:
Code:
void Die () {cout << "Bad Guy died";}
void Live () {cout << "Bad Guy Lives";}
.
.
.
void Process_Bad_Guy_State (int health, void(*fpointer) ())
{ if (health < 0)
{
//// if health is less than 0, then call the
//// function you passed in
(*fpointer)();
}
}
.
.
.
Process_Bad_Guy_State (health, Live());
//Or would it be "Process_Bad_Guy_State
//(health, Live);"
Is that how it would work? (actually it isn't...I already tried https://www.sharkyforums.com/images/.../2005/08/2.gif ) Any additional help would be appreciated.
[This message has been edited by Sheep (edited September 29, 2001).]
-
i hope this is what you are looking for. you could change the function types to anything you wanted, although im not sure if void would work. good luck
#include <iostream.h>
int *fun1(), *fun2();
void get_status( int, int *() );
int main()
{
int health;
cin >> health;
get_status(health, *fun1);
return 0;
}
void get_status( int hps, int *func() )
{
if( hps < 0 ) cout << "smell my dentures" << endl;
else *func();
}
int *fun1()
{
cout << "this is pointer 1" << endl;
return 0;
}
int *fun2()
{
cout << "this is pointer 2" << endl;;
return 0;
}
------------------
--obligatory pc specs--
Abit KT7 RAID
700 MHz Tbird
256 MB PC133 cheese
gainward geforce 3 :)
-
I don't know, but this:
might be making the compiler think you want a function called void(). (Just a guess) Also, this:
Process_Bad_Guy_State (health, Live());
should probably be this:
Process_Bad_Guy_State (health, Live);
(as you noted in the comment below it.) And if that doesn't work, you can always do stuff like this:
Process_Bad_Guy_State (health, &Live); https://www.sharkyforums.com/images/.../2005/06/5.gif
-
Thanks alot guys. I got it to work (vitaminj's example worked https://www.sharkyforums.com/images/.../2005/06/5.gif )
-
Really? Vitaminj's thing worked? I'm getting quite confused trying to read that. https://www.sharkyforums.com/images/.../2005/06/5.gif Could you explain some of that stuff to me, please?
For example, this:
get_status(health, *fun1);
is confusing to me. I read that as, you are calling get_status, and passing 'health' and that which is pointed to by 'fun1' as arguments. However, fun1 is a function, so if functions are really just pointers, then you'd really just be sending (a copy of) the entire function as a parameter, right? I'm not an assembly programmer at all, so try and bear with me. https://www.sharkyforums.com/images/.../2005/06/5.gif
Also, is there any reason in particular why your functions (fun1 and fun2) return int pointers? It would probably be more clear if they just returned int's, or void. https://www.sharkyforums.com/images/.../2005/06/5.gif
Third, in your declaration of get_status, the second parameter is 'int *func()'. So, it is taking an actual function that returns a pointer to an integer as a parameter. Then, when you call it (by "*func();"), well ... that actually does confuse me. I suppose that means that the entire function is passed to get_status, and then func() is created as a pointer to that function. Then "*func();" makes sense, since you really can put * in front of any function call.
So, am I thinking correctly, here? I really meant for this to be a question post, but I guess it just ended up just stating how I see that example. https://www.sharkyforums.com/images/.../2005/06/5.gif If my logic is correct, then your example DOES make sense, but it's just passing entire functions, not function pointers.
-
Wait...do you want me to explain it? I'm the one who doesn't get this https://www.sharkyforums.com/images/.../2005/08/2.gif So I guess you mean vitaminj, right https://www.sharkyforums.com/images/.../2005/06/5.gif
-
-
"int *fun1()"
That's weird, this looks like it should create a function "fun1()" that returns a pointer to an int. The syntax I'm used to seeing is:
"int (*fun1)()"
Or something like that. Or maybe it's implementation defined?
-
wow, im glad you could use that... ill try to explain it.
in get_status(health, *fun1)
1) i dont think im passing the entire fun1 function, im just passing the address.
the reason i did pointers and not just ints or voids was that otherwise id have to pass the whole function and not just the address (or so i think).
2) i did an int and not a void as the type because passing a pointer to a void could get pretty confusing for the compiler... let alone the programmer :P
really the reasoning behind the pointers (as i understand it) is that by passing just the address im giving the computer the starting point of the function then letting it follow through from there. im not really sure if this is whats happening, ive never done anything like that before, guess i should thank sheep https://www.sharkyforums.com/images/.../2005/06/5.gif as i understand it though from assembly this seems to make some sense.
3) this part probably makes sense by now but ill explain it just in case someone can prove me wrong :P i use the int *() declaration since im passing the address of the first instruction fo the function. This way the compiler knows to just start there and keep going until it gets an order to return back.
i hope this makes some sense, if not please feel free to ask about any strange parts
------------------
--obligatory pc specs--
Abit KT7 RAID
700 MHz Tbird
256 MB PC133 cheese
gainward geforce 3 :)
-
Okay, here's my version of your program: https://www.sharkyforums.com/images/.../2005/06/5.gif
Code:
#include <stdio.h>
#include <stdlib.h>
void get_status(int hps, void (*func)(void))
{
if (hps < 0)
printf("smell my dentures\n");
else
func();
}
void fun1(void)
{
printf("this is pointer 1\n");
}
void fun2(void)
{
printf("this is pointer 2\n");
}
int main()
{
get_status(3, fun2);
system("PAUSE");
return 0;
}
You can change get_status's parameters to be anything you want. (You can also pass &fun2 if you want, but it's not necessary -- also, you can call func() like this: *func(), but that's not necessary either)