#### Archived

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

# C++ and Function Pointers

This topic is 5749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I spent a long time last night trying to figure this out. Maybe my design is flawed, but I would like to know if what I am trying to do is at least possible. I have an input class, and I want to be able to load in keymaps etc without having to change the program, so I have decided on this method. Create an input manager class that creates my input object and then calls the update method to gather the keypresses. Fine so far, now I want to pass the Inputmanager class my player object and assign the playerobjects function pointer, at the moment defined as: void(cInputManager::*pfnKeypress)(TKEYDATA); I have to declare my cInputManager class in my cPlayer class, which I am not overjoyed about but can live with. All this is so when I call my players move object, it will in turn call the function pointer, update the keypresses and know which of its keys have been pressed. My logic makes sense to me... Now I am having a problem, I can''t call the function pointer. If I give my cPlayer class a pointer to the cInputManager class I can do this (inputmanager->*pfnKeypress)(keys); but woops, I think I am doing something wrong there.. cos it crashes if I call my players move method(which calls the keypress function). Now again, my design was thought up at about 12:30am last night, so I was a little tired.. I haven''t really had much experience of function pointers, but I knew I couldn''t make my inputmanager functions statics (as they need to call the inputmanagers update). If you can see what I am trying to do, and can suggest a better design then I have no problems with that, if you can help me work out what I am doing wrong, I would be most grateful. Either way, thank-you for any help you can provide. =*= If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore =*=

##### Share on other sites
Aren''t things a little backward here? Shouldn''t the player move method be called in response to input, basically implying that the move method is called by the keypress method (directly or indirectly)? So shouldn''t player objects be "registered" with the input manager which "dispatches" messages to the players upon receiving specific input?

  // rough, top-of-my-head codeclass InputManager{private:  struct move_method  {    Player * p;    (Player::*move)(void * param0, void * param1 );    void operator ( void * param0, void * param1 )    {      p->*move( param0, param1 );    }  };   typedef std::map< unsigned long, move_method > method_map;  typedef std::map< unsigned long, move_method >::iterator method_map_iterator;   method_map MMap;public:  move_method AddMethod( unsigned long keycode, move_method new_method )  {    method_map_iterator iter = MMap.find( keycode );    MMap[ keycode ] = new_method; // you can also use insert here    if( iter != MMap.end() )      return iter->second;     return 0;  }};

Now you''d use AddMethod to connect a particular keycode (however you choose to define keycodes) to a particular Player instance and method.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632936
• Total Posts
3009310
• ### Who's Online (See full list)

There are no registered users currently online

×