I have a function pointer defined by:
typedef void (*EventFunction)(int nEvent);
Is there a way to handle that function with a specific instance of a C++ object?
class A { private: EventFunction handler; public: void SetEvent(EventFunction func) { handler = func; } void EventOne() { handler(1); } }; class B { private: A a; public: B() { a.SetEvent(EventFromA); } // What do I do here? void EventFromA(int nEvent) { // do stuff } };
Edit: Orion pointed out the options that Boost offers such as:
boost::function<int (int)> f; X x; f = std::bind1st( std::mem_fun(&X::foo), &x); f(5); // Call x.foo(5)
Unfortunately Boost is not an option for me. Is there some sort of ‘currying’ function that can be written in C++ that will do this kind of wrapping of a pointer to a member function in to a normal function pointer?
I highly recommend Don Clugston’s excellent FastDelegate library. It provides all the things you’d expect of a real delegate and compiles down to a few ASM instructions in most cases. The accompanying article is a good read on member function pointers as well.
http://www.codeproject.com/KB/cpp/FastDelegate.aspx