Sign in to follow this  

Calling object methods without knowing "this"

This topic is 4598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this