Callback (TIMERPROC) to a c++ class function ??
Hi there,
Just wondering if its possible in any way to give a function pointer from a class to a C callback.
Ex: I got the class "Animation" having the member function "Draw()":
Now there the SetTimer() APi from Window, which requires a callback function (function pointer) to execute each time the timer times-out.
Could it be possible to pass the a Draw function from an INSTANCIATED "Animation" class to the SetTimer() function ??
Ex : SetTimer(...,...,...,&myAnimationInstance.Draw);
Re: Callback (TIMERPROC) to a c++ class function ??
Quote:
Originally Posted by
pepsidrinker
Hi there,
Just wondering if its possible in any way to give a function pointer from a class to a C callback.
Only if the function is static. If it isn't, then no.
Instead of giving the answer as to why (a non-static member function pointer includes a hidden this parameter), I'll ask you to try youself and call a non-static class member function using a pointer. In other words, pretend to be the function that will make the callback, and try to invoke the callback. You will quickly see that the syntax required to make such a call is way beyond what Windows could invent.
http://www.parashift.com/c++-faq/poi...o-members.html
Quote:
Ex: I got the class "Animation" having the member function "Draw()":
That's where you are wrong or making the wrong assumptions. What if you have two Animation objects?
Code:
class Animation
{
public:
int Draw();
};
int main()
{
Animation a1;
Animation a2;
}
So let's say you want Windows to call Draw(). Does Windows call a1.Draw() or a2.Draw()? See the problem. Two different instances, which one does Windows choose to call Draw() with?
That's why static member functions work, since there is one function shared between all instances of Animation.
Regards,
Paul McKenzie
Re: Callback (TIMERPROC) to a c++ class function ??
My Friend,
Thanks a lot for your detailed answer.
I was wondering : static function in classes are shared between instance , non-static function are not.
So setting the callback to , let's say:
Animation a1;
SetCallback(a1.Draw);
instead of
SetCallback(Animation::Draw);
That would solve the "many instanciations" problem, wouldn't it ?
I guess my knowledge of how memory manage c++ classes is a bit rusty, but if you don't mind enlighting me on why a pointer to a non-static function in an INSTANCIATED object absolutely needs to be declared with the class scope, that would help me a lot.
ie , why a pointer to
a1.Draw() ;
!=
void Draw();
Thanks again for your answer, it is appreciated.
Re: Callback (TIMERPROC) to a c++ class function ??
Quote:
Originally Posted by
pepsidrinker
So setting the callback to , let's say:
Animation a1;
SetCallback(a1.Draw);
That should be:
Code:
Animation a1;
SetCallback(&Animation::Draw);
Quote:
That would solve the "many instanciations" problem, wouldn't it ?
If the Draw function is static, it knows nothing about individual Animation object instances.
Code:
class Draw
{
public:
int x; // non-static member
static void Draw()
{
x = 1; // error
}
};
The Draw() function, since it's static, cannot access x, since x only exists for Animation object instances, and there is no instance of an Animation object for static functions.
Quote:
I guess my knowledge of how memory manage c++ classes is a bit rusty, but if you don't mind enlighting me on why a pointer to a non-static function in an INSTANCIATED object absolutely needs to be declared with the class scope, that would help me a lot.
I would have thought the link I gave you answered most, if not all questions concerning pointers to member functions.
Regards,
Paul McKenzie