Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help w/ Funcuntion Pointers

Started by gandolf1212 Dec 13, 2001 at 1:21 AM 6 replies 350+ views
Original Post
gandolf1212
gandolf1212
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!
I think there's to much blood in my caffeine system.
LyLFox
LyLFox
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.
gandolf1212
gandolf1212
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
I think there's to much blood in my caffeine system.
gandolf1212
gandolf1212
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.
I think there's to much blood in my caffeine system.
Oluseyi
Oluseyi
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!
Rube
Rube
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.

Topic Locked

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

Sign in to reply to this topic.