Re: How to Declare Pointer to Member Function?
jp2code wrote:
I have a thread that needs to make data visible to a function inside a
separate class.
In the past, I was using PostMessage to get my information to the class
function, but occasionally a message gets lost this way.
I could declare the class function as static, but then the function would
not be able to interact with other parts of the class.
The data is thread safe.
// header:
typedef void (*PtrToLpMsg)(LPTSTR lpMsg);
PtrToLpMsg g_AddStatusMsgFn;
// code:
CMain::CMain()
{
g_AddStatusMsgFn = AddStatusMsgA;
}
void CMain::AddStatusMsgA(LPTSTR lpMsg)
{
// other code
}
jp2code:
I have to tell you, I do not understand this question at all.
First of all using PostMessage (or SendMessage) and calling a function
directly are not normally alternatives that you can just switch between
at will, because the former changes the thread context, while the latter
doesn't. Remember, C++ has no concept of threads, and there is no such
notion as "methods of this class run in the primary thread".
Second, in order to call a class method via a pointer-to-member, you
need a pointer to the object, so why don't you just call the function
directly, if that is what you want (but it may not be, see above).
Third, you seem concerned that you have already used the LPVOID
parameter of the thread creation. Why? You can always pass a pointer to
an object of a class (or struct) that contains any information you may
need. It's API's that do not have such an LPVOID parameter that require
the use of global variables.
--
David Wilkinson
Visual C++ MVP