[Nottingham] C++ Programming

Robert Davies rob_davies at ntlworld.com
Thu Oct 16 10:43:58 BST 2003


On Wednesday 15 Oct 2003 21:35, David Bean wrote:

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

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

Each object  'instance of the class' can contain a private lookup table object  
for the implementation of key event/id mapping.  The  C++ STL contains a 
number of template classes, which provide lookup and insertion.

The classes, contain the member functions (actually an object stores just a  
pointer to a table of the member functions) so 'call a method in the class 
instance' sounds like you're hoping to have customised member functions in 
each object, which you cannot.

The member functions should be written to depend on the private class data, 
the registration sounds like it can be handled by storing a pointer to the 
object that registers the ID, which would allow you to call that object's 
member function with the -> syntax.

3 days is a very short time to get to grips with C++, Stroustroup's pages at 
http://www.research.att.com/~bs/C++.html are worth browsing, and really 
working through the C++ Programming Language 3rd edition, parts 1 & 2 for a 
week going through small examples pays off, so you can grasp the bigger 
picture, rather than get lost in your project implementation details.

Rob




More information about the Nottingham mailing list