[Nottingham] C++ Programming

David Bean david at dbean.uklinux.net
Wed Oct 15 21:35:37 BST 2003


On Wed, 2003-10-15 at 19:08, Martin wrote:
> Try newsnet. There's some very active C and C++ groups on there.

Do you have a URL?

> Then again, you could always try the folk here. You might get some 
> surprisingly good answers...

OK then, here goes...

I'm using SDL. I have a Keyboard class which intercepts all the keyboard
events. I want to have various classes call a method of the keyboard
class and register an id. When a key is pressed it looks up the
event.key.keysym.sym in a table and see if it corresponds to an id. If
it does then I want it to call a method in the class instance that
registered the id (with the sym, in case it registers more than one
key).

  bool registerKey(Uint16 sym, bool(*func)(Uint16 sym));
I thought something like this could work, but it only works on static
functions. I want class instances to register individual keys, so if I
have a button class, each instance of the button class can ask for a
different id/be mapped to a different key.
 
Something like:

  Button::Button(Keyboard k, Uint16 id) {
    k.registerKey(id, &keyPressed(Uint16))
...
  }
  bool Button::keyPressed(Uint16 sym) {
... 
  }

  Menu::Menu(Keyboard k, Uint16 id) {
    k.registerKey(id, &keyPressed(Uint16))
...
  }
  bool Menu::keyPressed(Uint16 sym) {
...
  }

Does anyone have any code, or better ways of doing this? The point is to
make the keyboard mapping changeable at runtime without a big switch
statement.

> And in any case, what's wrong with function pointers so long as you keep 
> them typed?

They are not well explained in any of my books... ...and it's early days
for my c++ skills.

David Bean




More information about the Nottingham mailing list