Re: How to Declare Pointer to Member Function?
On Mon, 16 Jul 2007 13:39:06 -0500, "jp2code" <poojo.com/mail> 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.
It shouldn't.
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
}
This is how you do it. Note that to call a non-static member function
through a pointer to member function, you need a pointer or reference to an
object of the class to which the member belongs.
class CMain
{
public:
CMain();
void AddStatusMsgA(LPTSTR lpMsg)
{
}
};
typedef void (CMain::*PtrToLpMsg)(LPTSTR lpMsg);
PtrToLpMsg g_AddStatusMsgFn;
CMain::CMain()
{
g_AddStatusMsgFn = &CMain::AddStatusMsgA;
}
void f(CMain* p)
{
(p->*g_AddStatusMsgFn)(0);
}
The usual caveats about global data apply.
--
Doug Harrison
Visual C++ MVP