EventHandler* myEventHandler = new EventHandler(); // member of a class
myEventHandler += new EventHandler(&funcAddress);
class EventHandler
{
<PointerToFunctionObject> ptr; // this needs to be a type?
EventHandler* next;
...
// tag another one on to the chain (like a singly linked list)
void operator +=(EventHandler* newEventHandler)
{
if(next != NULL)
next = newEventHandler;
else
(*next) += newEventHandler;
}
// call the function and pass in a pointer to the calling class and args
void Handle(void* sender, EventArgs* e)
{
if(next != NULL)
{
ptr.Handle();
next->Handle(sender, e);
}
else
{
ptr.Handle;
delete(e);
}
}
}
I've not used function pointers or member function pointers before so it's been slow going, and as I said I dont have internet access. The MSDN help files with VS haven't been overly useful giving only a few examples. I would like to be able to build inheritance into the event handler system. The problem I think I'm having is that I would need a generic member function pointer which I could cast to the type of object that the pointer needs casting to when it is called. I don't know how to do that. I looked into using delegates in C++ but don't know if it would be suitable.
I found this article but didn't know if it would be useful or not. http://www.myelin.co.nz/notes/callbacks/cpp-delegates-without-helper.html
Got to go now. Would really appreciate any help on this. If I can't use function pointers, what can I use? I really like the design, but I dont know how to implement it in code exactly...
C++ "Message handlers"
Hi, I'm not sure I have too long to describe the problem as I'm on a public computer so I'll do my best (no internet access at home you see - moving house!)
Recently I began trying to write for C++ OpenGL a way of displaying 2D forms and form controls as part of game's user interface. I'm writing it from the ground up. I've recently been using VS8 developing a lot of C# and like the way that event handlers can be used to add functionality to a control. I set about writing my own EventHandler class, which I hoped would be able to contain a function pointer of some kind to a function that I would add. Each event handler would carry a pointer to the next event handler (until NULL) and so a chain of events could be built up each time I call:
Before rolling your own, you might want to consider existing event libraries like boost::signal, SigC++, or Sneftel's event library.
Hi. The C++ doesn't have delegates as part of the language. It has only function pointers. However the function pointers are different depending on if you are trying to point to the member function or a regular function.
I would suggest you look at fastdelegate "hack". I found it to be one of the best examples of delegates for C++
I would suggest you look at fastdelegate "hack". I found it to be one of the best examples of delegates for C++
Thanks DMINATOR. I actually came across that article the other day when I posted but didnt have time to look at it fully. I saved it to my Ipod to look at at home but never had time. I'll look it over again when I have time and let you know how I get on.
SiCrane, thanks for the help. I'll check out those links (save content for a later look). I know if it's been written before, there's not a huge reason for rewriting it but I'd like to use this as an exercise to expand my own skillset. I've not had a reason to use function pointers in a big way until now.
SiCrane, thanks for the help. I'll check out those links (save content for a later look). I know if it's been written before, there's not a huge reason for rewriting it but I'd like to use this as an exercise to expand my own skillset. I've not had a reason to use function pointers in a big way until now.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement