Question with function pointers in an interface class

I’m not sure how much this has to do with SDL… but I’m not sure where else
to ask. I’m having a lot of issues with a set of function handler and no
matter what I do I get compiler errors. The important variables are 'keyfn’
and ‘mousefn’ which are pointers to Sint32. The functions that call the
keyhandler and the mousehandler seem to bug out on me no matter how I write
them in the compiler.

I’ve tried the following:
return *(data.keyfn)(/args/);
return (*data.keyfn)(/args/);
return (data.*keyfn)(/args- this is what is there and what I thought was
right
/);
return data.*keyfn(/args/);

-Jim Stapleton.

CODE BELOW:

/in interfaceobject.h/

/include for SDL, and some precompile stuff/

class InterfaceObject
{
public:
typedef struct SInterfaceObjectData
{
/various other stuff/
Sint32 *keyfn (InterfaceObject, InterfaceObjectData,
SDL_KeyboardEvent);
/keyhandler function event handler/
Sint32 *mousefn (InterfaceObject, InterfaceObjectData,
SDL_MouseMotionEvent, SDL_MouseButtonEvent);
/mousehandler/

} InterfaceObjectData;

protected:
InterfaceObjectData data; /the data for this object, it’s better/
/*than having to pass all these things */
/*as arguments */

public:
/some unrelated functions were put here/

/*this tells the object how to handle input to it*/
virtual Sint32 keyboard_event(SDL_KeyboardEvent *event);

virtual Sint32 mouse_event(SDL_MouseMotionEvent *motion,

SDL_MouseButtonEvent *button);

/*this sets the function that the object will use to handle events*/
inline Sint32 set_keyhandler(Sint32 *fn);

inline Sint32 set_mousehandler(Sint32 *fn);

};

/defined constants used here (all caps beginning with IFO)/----------------------------------------------------------------------------

/in interfaceobject.cpp/

#include “interfaceobject.h”
#include <memory.h>

/definitions of unrelated functions here/

Sint32 InterfaceObject::keyboard_event(SDL_KeyboardEvent *event)
{
return (data.*keyfn)(this, &data, event);
}

Sint32 InterfaceObject::mouse_event(SDL_MouseMotionEvent *motion,
SDL_MouseButtonEvent *button)
{
return (data.*mousefn)(this, &data, motion, button);
}

inline Sint32 InterfaceObject::set_keyhandler(Sint32 *fn)
{
data.keyfn = fn;
}

inline Sint32 InterfaceObject::set_mousehandler(Sint32 *fn)
{
data.mousefn = fn;
}

Try a C++ faq. They are designed specifically to answer questions like
these. You can learn a lot of other things as well, such as the 'inline’
keyword shouldn’t go in front of a class declaration, but the definition
instead. If you insist on asking someone instead of looking it up yourself,
try a C++ newsgroup, such as alt.comp.lang.learn.c-c++.

-Jason> ----- Original Message -----

From: stapleton.41@osu.edu (Steven James Stapleton)
To:
Sent: Sunday, March 31, 2002 10:53 AM
Subject: [SDL] question with function pointers in an interface class

I’m not sure how much this has to do with SDL… but I’m not sure where
else
to ask. I’m having a lot of issues with a set of function handler and no
matter what I do I get compiler errors. The important variables are
’keyfn’
and ‘mousefn’ which are pointers to Sint32. The functions that call the
keyhandler and the mousehandler seem to bug out on me no matter how I
write
them in the compiler.

I’ve tried the following:
return *(data.keyfn)(/args/);
return (*data.keyfn)(/args/);
return (data.*keyfn)(/args- this is what is there and what I thought was
right
/);
return data.*keyfn(/args/);

-Jim Stapleton.

CODE BELOW:

/in interfaceobject.h/

/include for SDL, and some precompile stuff/

class InterfaceObject
{
public:
typedef struct SInterfaceObjectData
{
/various other stuff/
Sint32 *keyfn (InterfaceObject, InterfaceObjectData,
SDL_KeyboardEvent);
/keyhandler function event handler/
Sint32 *mousefn (InterfaceObject, InterfaceObjectData,
SDL_MouseMotionEvent, SDL_MouseButtonEvent);
/mousehandler/

} InterfaceObjectData;

protected:
InterfaceObjectData data; /the data for this object, it’s better/
/*than having to pass all these things */
/*as arguments */

public:
/some unrelated functions were put here/

/*this tells the object how to handle input to it*/
virtual Sint32 keyboard_event(SDL_KeyboardEvent *event);

virtual Sint32 mouse_event(SDL_MouseMotionEvent *motion,

SDL_MouseButtonEvent *button);

/*this sets the function that the object will use to handle events*/
inline Sint32 set_keyhandler(Sint32 *fn);

inline Sint32 set_mousehandler(Sint32 *fn);

};

/defined constants used here (all caps beginning with IFO)/


/in interfaceobject.cpp/

#include “interfaceobject.h”
#include <memory.h>

/definitions of unrelated functions here/

Sint32 InterfaceObject::keyboard_event(SDL_KeyboardEvent *event)
{
return (data.*keyfn)(this, &data, event);
}

Sint32 InterfaceObject::mouse_event(SDL_MouseMotionEvent *motion,
SDL_MouseButtonEvent *button)
{
return (data.*mousefn)(this, &data, motion, button);
}

inline Sint32 InterfaceObject::set_keyhandler(Sint32 *fn)
{
data.keyfn = fn;
}

inline Sint32 InterfaceObject::set_mousehandler(Sint32 *fn)
{
data.mousefn = fn;
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl