Archived

This topic is now archived and is closed to further replies.

Function pointers...

This topic is 5115 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

Yes I did a search for it both in the forums and articles. My issue is related to they differences between declaration and the function I am trying assign to it. I have a class that holds all events that is a singleton. However the class that tries to consume this errors on compile time. Saying it doesn''t match the declaration. typedef long(*EventDelegate)(); EventSubSystem *events = EventSubSystem::GetInstance(); Event event; event.event = 0x0001; event.eventFunc = NetworkEvents; //errors here... events->RegisterEvent(event); void NetworkSubSystem::NetworkEvents() { } Not really sure how to resolve this. Is this approach even a good idea?

Share this post


Link to post
Share on other sites
Of course, any function in a class automagically passes the "this" pointer, so the function types are different.

Either make NetworkEvents static, which means you don''t have access to the class members, or look for a way to make the function pointer point to a member of a class.

Share this post


Link to post
Share on other sites
Pointer to member function syntax:

class Foo { public: long Bar(); };
typedef long (Foo::*pmf)();

Foo foo, *pfoo;
pmf ptr = &Foo::Bar;

foo.*ptr();
pfoo->*ptr();


foo.*ptr and pfoo->*ptr) cannot be manipulated in any way beside being called. You have to store the object and the pointer-to-member separately (add a new field to your Event type).

Share this post


Link to post
Share on other sites