dimanche 31 janvier 2016

C++ multiple callback member functions from different classes without std and boost

I'm new to C++ and I don't want to use any libraries because I want to really understand whats going on internally. Additionally I try to keep my library-use as low as possible to improve performance.
So all in all I'd like to not use std or boost to solve this problem.

My question:

I have an EventHandler which has a function:

template<class T> void EventHandler::SetCallbackFunction(T* obj, void (T::*mem_fkt)(void));

I have to save the object and the function to call it later.
There are 2 possibilities:

  • EventHandler as template class (and typedef)
  • void* -> but to call the function I need to cast it back and for that I need the classname :/

Why these dont work: - The EventHandler can't be a template class because it needs to handle multiple classes.. - void* -> I don't know how to save the classname to cast it later on

How can I do that?
(Or is there another way? Maybe a way to save the classname to cast it later on?)


And I know that you could solve it based on something like this:

class A{
  public:
    void Callback();
    static void CallbackStatic(void* self){
      static_cast<A*>->CallBack();
    };
};

But this restricts me too much for my taste.

Thanks for any suggestion :)

Aucun commentaire:

Enregistrer un commentaire