Advertisement

Is there a way to get pointers to functions?

Started by March 17, 2000 03:16 PM
10 comments, last by Chrono999 24 years, 11 months ago
Here''s my situation, I have a function which initialises the AI of an enemy. Depending on the number you pass to it the main function of the enemy will call different tactic functions. (I can''t explain it very well so this is basically what I want) void tactic1() { //enemy zigzags across screen } void tactic2() { //enemy flies straight across screen } void InitAI(int tactic) { //if you enter one then a pointer(?) points to tactic one } void RefreshEnemy() { //call the tactic that was chosen in init AI } Thanks for you''re help anyone who answers. If you can have pointers to functions then how do you declare them?
Yes, you can put a (probably global) variable that looks something like :

void (*func)();

and then you can just say func=tactic1, and then in RefreshEnemy just call func(), this syntax works for me in Borland C++, but you might have to make it func=&tactic1 depending on your compiler. Just remember that the return types and parameters must match between the pointer and the functions it''s pointing to.
Chris
Advertisement
#include
#include

int maths(int (*math)(int a, int b), int a, int b)
{
return(math(a,b));
}

int multiply(int a, int b)
{
return(a*b);
}

int add(int a, int b)
{
return(a+b);
}

main()
{
int a, b;

int (*f)(int a, int b);

f = multiply;

cout<
f = add;

cout<
cout<<"press any key to exit";

getche();
}
"I have realised that maths can explain everything. How it can is unimportant, I want to know why." -Me
Thanks for your help but I''m getting an error.(I''m using VC++) here''s my code:

void (*func)();

int Tactic1()
{

}

void InitAI(int mode)
{
func=&Tactic1
}

It gives me this error:
error C2276: ''&'' : illegal operation on bound member function expression
Try it without the &
Chris
It gives me this error:
error C2440: ''='' : cannot convert from ''void (__thiscall Enemy::*)(void)'' to ''void (__cdecl *)(void)''
There is no context in which this conversion is possible
Advertisement
Remember that the return types have to be the same, so make it int (*func)()
Chris
From the error message, it looks like Tactic1 is a part of a class Enemy, I don''t think that works on classes. (Or you have to do it differently anyways)
--If Train A leave San Francisco at 8:30am EST travelling 25mph and Train B leaves Chicago at 1:30pm MST travelling at 40mph, and they're 3000 miles apart when they start, what is the capital of Bulgaria?
Yeah, it''s part of a class. Can it still be done
yes it can be done, instead of just void (*func)() or whatever you have do void (Enemy::*func)(), and when you assign it to a function do something like func = &Enemy.Tactic1, and to call it, make an object of enemy and do

Enemy ene;
(ene.*func)();

but remember to initialize it first or you''ll get a general processor fault, this is all I know, sorry if that doesn''t help
Chris

This topic is closed to new replies.

Advertisement