Calling object methods without knowing "this"

Started by
1 comment, last by Illco 18 years, 11 months ago
I am (trying) to implement an Event class template which would work like a C# Event. That is, first I declare it: Event<ClickEventArgs> OnClick; then I add handlers to it void MyClickHandler(ClickEventArgs args); ... OnClick+=MyClickHandler; the I invoke it ClickEventArgs args=new ClickEventArgs(x,y); OnClick(args); and it calls all the handlers with the specified params The problem is that I want the event handlers to be non-static object methods. Is there a way to implement this without using crappy solutions like passing the reference to the object with to the event handler and making it static?
May the sun shine upon you
Advertisement
Hi Sheeva_

not sure if I fully understand your problem, but you might want to give the boost libraries a try:

Boost.Function is very good at wrapping up functions of various types - they can be incredibly useful when implementing a callback system.
Boost.Signals encapsulates a signals and slots implementation, which can also be used to write an event system (a prototype of my phd project used this library).

I'm sorry if your problem is a little more complicated than this, but couldn't you also use a std::mem_fun (or similar) to form a pointer or reference to a member function?

Hope this helps :)
The OO-approach presents another alternative. You could create an interface that defines the handler:
class IHandler{public:   void Handle( ... );}

Then store the registered objects (non-statically) and call the handlers when necessary. You'd probably have to do some work to make this work with arbitrary event arguments though.

Greetz,

Illco

This topic is closed to new replies.

Advertisement