Jump to content
  • Advertisement

Archived

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

firecast

callback functions driving me nuts!!

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

hi!, i''ve been searching all over the net and i couldn''t find A THING about how to implement callback functions. i want to make a callback member function on my class, but i don''t know how to do it (i wanna make a timer class that handles the events by itself) so can you help me?? HOW DO I IMPLEMENT A CALLBACK FUNCTION?? thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
I think that class member can not be a CALLBACK function.
But i''am not 100%sure :D




"The Gods Made Heavy Metal And They Saw That It Was Good They Said To Play It Louder Than Hell We Promised That We Would
When Losers Say Its Over With You Know That It’s A Lie The Gods Made Heavy Metal And It’s Never Gonna Die"

THE GODS MADE HEAVY METAL/by
ManOwaR

Share this post


Link to post
Share on other sites
http://function-pointer.org

Class memebers may be callbacks, check out the other threads on this topic.

Share this post


Link to post
Share on other sites
Assuming that you, like the hundreds of posters before you who asked the very same question (without bothering to do a forum search), want to use a non-static member function as callback, here is the minimum code to "make it work" - that is, how to make a call via a pointer to member. Pointers to static members really are just the same as pointers to non-members.


class Foo
{
public:
int bar(int);
};

int (Foo::*mfptr)(int);
mfptr = &Foo::bar;

Foo obj;
Foo* ptr = new Foo;

(obj.*mfptr)(10);
(ptr->*mfptr)(10);


The callback API will have to be designed specifically to accept pointers to Foo members, not just ordinary pointers. If it hasn't (e.g. Win32 API), it is simply impossible to make it work in C++.

You will also have to pass the pointer and the object separately to your callback API and let it rejoin them at the last moment. The expression obj.*mfptr is known as a bound pointer to member and cannot be manipulated in any other way but immediately calling it. No assigment to a variable, no passing to a function, no nothing.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on February 6, 2004 3:38:09 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!