
static function access member variable and member function
Quote:
>hi, i hv a c function library which require me to pass a callback
>function pointer into it. since that parameter cannot accept member
>function, i try to solve it by passing static function pointer.
>--------------------------------------------------------------------------
-----------------------
>class CXFaceRecognitionDlg : public CDialog
>{
>// Construction
>public:
> CXFaceRecognitionDlg(CWnd* pParent = NULL); // standard constructor
> static void callback(IplImage* image);
>}
>void CXFaceRecognitionDlg::callback(IplImage* image)
>{
> DetectAndDrawFaces( image ); // c function lib
> //????but how to update CXFaceRecognitionDlg GUI????
>}
>-------------------------------------------------------------------------------------------------
>but the problem goes on. i need to do some CXFaceRecognitionDlg GUI
>update inside the static function. but since static callback function
>can only access to static function and variable, there are no way for
>me to access the member function and variable of CXFaceRecognitionDlg.
>can anyone tell me how can i solve this problem?
Many callbacks take a user-defined pointer parameter, which is typically a
pointer to whatever additional arguments or state the callback requires. For
static member function callbacks, it's common to pass the "this" pointer to
the function which calls the callback, e.g.
void callback_user(void (*f)(void*), void* data)
{
f(data);
Quote:
}
struct X
{
void f()
{
callback_user(Callback, this);
}
static void Callback(void* p)
{
X* pThis = static_cast<X*>(p);
}
Quote:
};
If your callback_user function doesn't support this user-defined parameter,
you'll need to use a global, and pay special attention to whatever
multithreading or reentrancy issues may arise.
--
Doug Harrison
Microsoft MVP - Visual C++