[Nottingham] C++ Programming

Alex Tibbles alex_tibbles at yahoo.co.uk
Thu Oct 16 11:34:01 BST 2003


 --- David Bean <david at dbean.uklinux.net> wrote: > 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?
see my other email and duncan's

> 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) {
> ... 
>   }

you want mem_fun:

k.registerKey(id, mem_fun(&keyPressed))

It's normal btw to pass non-build in types by (const
if applicable) reference:

Button::Button(Keyboard& k, Uint16 id) {...}

otherwise you will register with a *copy* of the
passed in Keyboard object.


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

ditto

> 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.

I hope this helps.

alex

________________________________________________________________________
Want to chat instantly with your online friends?  Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk



More information about the Nottingham mailing list