Using Non-Static Callbacks as member functions VC 5.0 
Author Message
 Using Non-Static Callbacks as member functions VC 5.0

HELP!

Until the release of Visual C++ 5.0, we were able to use member functions
as callback's without declaring them static.  From what I have read in the
Microsoft Knowledge Base Article Q148789, the ability to use member
functions as callbacks without making them static was in fact a bug in
Visual C++ 4.0 - 4.2 and that to use a member function in this manner is
against the Ansii C++ standard.

So within Visual C++ 5.0 I must make these callbacks static to get the app.
to run and compile.  The limitations caused by the lack of an implied this
pointer in a static function of course makes application design a good deal
more convoluted.

IF ANYONE  can tell me how to use a member function as a callback function
without declaring it static, I would greatly appreciate the input.  Here is
a sample of what worked in Visual C++ 4.2:

Definitions:

#define RM_PATHPROC FAR __cdecl EXPORT

Function Prototype:

typedef int (RM_PATHPROC * LPMEMBERFN3DPATHPROC)(LPWORLDPARAMST, DWORD,
WORLDPT3DST);
typedef int (RM_PATHPROC * LPFN3DPATHPROC)(C3DAnimationObject *,
LPWORLDPARAMST, DWORD, WORLDPT3DST);

////////////////////////////////////////////////////////////////////////////

/
// C3DFunctionPath - Path is described by a function in world coordinates.
class  C3DFunctionPath : public C3DPath, CFunctionPath
{
public:

// Constructors

    C3DFunctionPath(LPMEMBERFN3DPATHPROC lpfn3DPathProc, WORLDPT3DST
wpt3dInitial);
        C3DFunctionPath(LPFN3DPATHPROC lpfn3DPathProc, WORLDPT3DST wpt3dInitial);

// Implmentation
    virtual int UpdatePosition(LPWORLDPARAMST lpWorldParamSt,
C3DAnimationObject * pAniObject);

public:
    virtual ~C3DFunctionPath();

protected:    
//  DWORD dwDeltaTime;
    WORLDPT3DST wpt3dInitial;             // starting point of path
    LPFN3DPATHPROC lpfn3DPathProc;

Quote:
};

At this point a class will be defined to use the LPMEMBERFN3DPATHPROC
ie:

class CCamera : public C3DAnimation
{
        .
        .
        .
        C3DFunctionPath *m_pCameraPath_0;
        int RM_PATHPROC Camera_Proc_0(LPWORLDPARAMST lpWorldParamSt,
                                                        DWORD dwDeltaTime,
                                                        WORLDPT3DST wptstInitial);

Quote:
}

Within the body of the implemenation, there will be something like this:

CCamera::CCamera()
{
        .
        .      
        .
        m_pCameraPath_0 = new C3DFunctionPath(Camera_Proc_0,wptstCameraInitial);

Quote:
}

int RM_PATHPROC CCamera::Camera_Proc_0(LPWORLDPARAMST lpWorldParamSt,
                                                                DWORD dwDeltaTime,
                                                                WORLDPT3DST wptstInitial)
{
        .
        .
        .
        return 0;

Quote:
}

With the above setup I am able to have a member function callback that has
access to all of the member variables of class CCamera through the implied
this pointer.  With this I can create multiple objects of this class type,
and have a common callback that is able to access the data members for the
given instance of the object.  Using this callback as a control routine is
then very easy to design.

The problem with VC++ 5.0 comes to light by forcing me to make these
callbacks static.  Without an implied this pointer I am unable to access
the data members directly and therefore cannot use this callback as a
common control callback for multiple instances of the same object. (I hope
this makes sense!).

Thanks in Advance.

Cliff Kondratiuk, PEng.
Rainmaker Digital Pictures Group.



Tue, 21 Sep 1999 03:00:00 GMT  
 Using Non-Static Callbacks as member functions VC 5.0

You really can't use a non-static member function as a call back because of
the implied "this" parameter. The solution is to use a static member
function or even just a global function and pass it an *explicit* this
pointer. In other words pass the callback function a pointer to the object.
The callback function can then access all of the public members in the
class or it could simply call a (non-static) member function in the class
to do all of work.

David



Tue, 21 Sep 1999 03:00:00 GMT  
 Using Non-Static Callbacks as member functions VC 5.0

The bug talked about in Q148789 is that the compiler didn't generate an
error for doing this.  If you had it working properly (non-static member
functions as callbacks) then you where plain lucky.  The problem as
pointed out in other replies is that the function is not getting a
"this" pointer passed into it, hence your stack is screwed up from the
point the function starts to execute.  Technically speaking your
funciton should crash your program, you just getting lucky that it's not
--
I have no beliefs....
                I believe I'm a walking contradiction....



Wed, 22 Sep 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

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

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

3. CALLBACK and non-static member function problem!

4. using non-static member functions using delegates

5. illegal call of non-static member function

6. illegal call of non-static member function

7. illegal call of non-static member function *HELP*

8. iilegal call of non-static member function

9. illegal call of non-static member function

10. Illegal call of non-static member function (error)?

11. illegal call of non-static member function

12. Error C2352 - illegal call of non-static member function

 

 
Powered by phpBB® Forum Software