Jump to content
  • Advertisement
Sign in to follow this  
Sheeva_

Calling object methods without knowing "this"

This topic is 4905 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
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 :)

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!