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
Actually, it''d probably be best if you set a default pointer, such as

int (Enemy::*func)() = &Enemy::Tactic1;
Chris
In looking at other answers given, maybe they have a cleaner answer than what I found out. Mine works that''s all I can say.

I went through this problem a couple of weeks ago. Ordinary functions like
void PlayerAction(int ms_flags)
are static in their nature and when you have it, you have only one instance. With member functions though, you have them tied into the data that is instantiated many times with many pointer locations. You need to know where that instance of the member function is located and the method recommended to me was having an intermediate function that
called the instance.

Declare the member function type
typedef void (Actor::*pfnActionFn)(Actor *pActor, int ms_flags);

Inside the class declare
pfnActionFn *pAction

and then later set the action you want with
pAction = Walking

this is the function you call each frame
pCurrentGuy->DoAction(pOtherGuy, ms_flags);

void Actor::DoAction(Actor *pActor, int ms_flags)
{
(this->*pAction)(pActor, ms_flags); // Note ->* operator
}

You have to think of the brackets around=> (this->*pAction) as being there to evaluate rather rather to just "protect". The ''*'' is indirection that points not to the pointer itself but to evaluating the contents of the function pointer.

Look at my thread that covered this topic as I worked my way through it



ZoomBoy
A 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor and diary at
Check out my web-site

This topic is closed to new replies.

Advertisement