views:

154

answers:

3

I'm doing some research in how to implement a event-handling scheme in C++ that can be easyest as its to implements an adpter to a class in java. The problem is that with the approach shown below, I will need to have all adapters already implemented with its function overriding in the devived class (because the linker needs it). On the other side, using a delegate strategy where I can use the adapter just in the derived class should imply in less performance considering the way it need to be implemented.

wich one, or what on else should be the best approach to it?

class KeyboardAdapter  
{  
public:  
    virtual void onKeyDown(int key) = 0;  
}

class Controller : public KeyApadter  
{  
private:  
   void onKeyDown(int key);  
}  

void Controller::onKeyDown(int key) {}

class UserController : public Controller {
private:
    void onKeyDown(int key);
}

void UserController::onKeyDown(int key) {
   // do stuff
}

int main() {
  UserController * uc = new UserController();
  Controller * c = uc;
  c->_onKeyDown(27);
}
A: 

Given that your code appears to be handling keystrokes from the user, and given that nobody types faster than, say, 100-150 words per minute, I wouldn't worry too much about efficiency. Just do it the way that makes the most sense to you.

Jeremy Friesner
you are right in part. It should be used to process user input in a win32 opengl app, keyboard and mouse, and it need to save as much time as it can i believe.
Ruben Trancoso
+1  A: 

Take a look at Boost.Signals library for an example of how you can implement event handling without classes with virtual functions (http://www.boost.org/doc/libs/1_39_0/doc/html/signals.html).

alexeiz
A: 

Besides boost::signals, you can try sigc++. It is used by the C++ GTK/Glib wrapper GTKmm. It seems to fit your needs.

neuro