static function access member variable and member function 
Author Message
 static function access member variable and member function

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);

Quote:
}

void CXFaceRecognitionDlg::callback(IplImage* image)
{
        DetectAndDrawFaces( image );    // c function lib
        //????but how to update CXFaceRecognitionDlg GUI????

Quote:
}

-------------------------------------------------------------------------------------------------
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?

thank you.

regards
yccheok



Sun, 13 Nov 2005 07:07:23 GMT  
 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++



Sun, 13 Nov 2005 07:20:48 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. strange problem occur while trying to access member function through static function

2. Giving static member functions access to protected functions

3. member function and member variable

4. member function and member variable

5. member function and member variable

6. Private member access in a different function member

7. C2086 error: static local variables in member functions

8. static variable defined in a member function

9. Getting pointer to non-static member function from C callback function

10. static function calles member function?

11. Using Non-Static Callback Functions as member Functions VC5.0

12. Accessing member variables in a thread function...

 

 
Powered by phpBB® Forum Software