Archived

This topic is now archived and is closed to further replies.

gandolf1212

Help w/ Funcuntion Pointers

Recommended Posts

gandolf1212    100
Okay, basically what i''m trying to do is delare a function pointer in a class that points to a function in the same class. Something like this...
class Creature{
 public: 
  int X;

  void (*Move)();

  void Walk();
  void Run();
};

void Creature::Walk(){ // Normal movement
  X += 1;
};

void Creature::Run(){ // Fast movement
  X += 5;
};

void Main(){
  Creature C1;

  C1.Move = C1.Run; // Now i try to set the pointer to my
                    // function and it crashes
};
If anyone knows how to do this please tell me, this has been bothering me for weeks. Thanks!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Looks to me like your thinking "in C" and not "in C++" - rather than assign Run or Walk to Move just call Move and have Move determine whether Run should be called or Walk should be called

Move Method

if conditions indicate running
call run method
if conditions indicate walking
call walk method

etc...

Share this post


Link to post
Share on other sites
LyLFox    122
The reason why you''re getting error is because void (*Move)() is a pointer to a function, and when you declare C1.Move = C1.Run you are forcing C1.Move to point to a member function. A pointer to function is different from a pointer to member function. If you want that to work, you''ll have to declare C1.Move() to be a pointer to a member function - although I agree with AP''s suggestion since that it''s much simpler and less overhead.

Share this post


Link to post
Share on other sites
gandolf1212    100
quote:
Original post by LyLFox
you''ll have to declare C1.Move() to be a pointer to a member function


Could you please tell me the syntax to do that?


Also, the reason I''m trying to do this is because I want to create a game with a very large amount of moves and a big gaint switch statement seems a little messy compared to a function pointer array, plus you loose some cool array/matrix math tricks.

Thanks alot for the help guys,
Gandolf

Share this post


Link to post
Share on other sites
gandolf1212    100
Oluseyi, i went to the sight you gave me but i can''t figure out what to do. I put in the line:

void (Creature::*Move)();

and set move to Walk and Run (plus a few other changes) but much crashing occured. Thanks anyway though.

Share this post


Link to post
Share on other sites
Oluseyi    2103
The point (pun not intended) is that your pointer to methods should be outside the class definition. If it''s inside the class, why not have the user set a flag ro have the method do some additional logic?

Outside class:
void (*Creature::Move)();
Creature c1;
// assume object has been initialized
Move = c1.Run();
...
c1.Move(); // <- this is how you use the method pointer


[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Rube    122
Hrms, I don''t know exactly what you mean...but how about this:

Create an array of all your movement rate values you want to store as a property of your class

Then create a function ::SetMovementRate(unsigned int array_index)

Then within your function, edit a value for your movement rate

current_move_rate += movement_array(array_index);

or whatever you are doing.

It''ll preserve your array tricks?

R.

Share this post


Link to post
Share on other sites