Re: a callback function across the dll boundary
On Sat, 24 Jun 2006 07:08:33 GMT, "Tony Young" <jdt_young@yahoo.com> wrote:
Hello Doug,
Thank you very much for the help. One thing I don't understand is why you
changed CallBackFunc(int nRet) from a class member function to a static (or
global) function.
I changed it to a static member function so that it would be compatible
with the function pointer type you went on to use:
typedef void(*funcptr)(int);
The corresponding non-static member function typedef would have been
written like this:
typedef void(CMain::*funcptr)(int);
Is it a must?
For the typedef you defined, yes.
There's a big difference between a "pointer to member function" (the
non-static quality is implied by the term) and a normal function pointer.
An instance of an object is required to call a non-static member, and you
need an object instance to call a pointer to member function. Calling a
global function or a static member function does not involve an object
instance, and both can be represented by a normal function pointer, which
is what you used. You could use a pointer to member, but then you'd have to
keep a pointer to an object through which you'd invoke the callback.
If I create variable m into heap (by the
following statement) instead, and make m outlive m_pSupport, then it's safe
to make CallBackFunc(int nRet) as a member function. Is it right?
CMain *m;
...
m = new CMain;
Your further help is much appreciated.
If you're going to refer to an object, the object has to exist when you
refer to it. If the callback is a pointer to member function, you need a
valid object on which to call it.
--
Doug Harrison
Visual C++ MVP