PDA

View Full Version : In C++, How can I use callback function?


torija
04-12-2002, 02:40 AM
ISOCKET_Connect(m_pICmdSocket,
m_inaddr, m_inport,
(PFNCONNECTCB)/*here*/,
(void *)this);

Connect callback function is CApp::ConnectCB.
What I fill in /*here*/?

CApp::ConnectCB, ConnectCB, this->ConnectCB and this->ConnectCB((void *)this) would not work.

fredch
04-12-2002, 07:07 AM
/* a "C" callback function that casts the arg */
/* then calls "into" the class */

extern "C"
void connectCallback (void *pUser)
{
CApp * app = (CApp*)pUser;
app->ConnectCB();
}


/* callback to the "C" function */
ISOCKET_Connect(m_pICmdSocket,
m_inaddr, m_inport,
(PFNCONNECTCB)connectCallback,
(void *)this);

------------------------------------------------------
Instead, you may be able to declare CApp::ConnectCB as static, but I haven't tried that with the ARM compiler, so I don't know if it would get the correct calling conventions.

torija
04-12-2002, 09:03 PM
Thanks a lot.

Your advice was a great help to me.

:)

mroskothen
05-16-2002, 12:31 PM
callback works with static functions and C++.

Does anyone know WHY callback does not work with non-static functions?

radub
05-16-2002, 05:42 PM
because you have an extra 'this' pointer