Click to See Complete Forum and Search --> : Function Pointer


flotat
April 16th, 1999, 11:23 AM
Hi !

I'm having problem when trying to declare a C++ function pointer.

I declared a structure :
typedef struct
{
char szTypeName[256];
void (*Func)();
}ITEM, *pITEM;

then a function called for example :

void CMyProject::GetDate()

and I'd like to use another function to set data for an item :

void CMyProject::Add( char *szItemName, void (*Func)() )
{
// Add a new data type
pITEM pData = new ITEM();
strcpy(pData->szTypeName,szItemName);
pData->Func = Func;
m_aDataType.Add(pData);
}

The following sample doesn't compile !!!!
ex : Add("DATE",GetDate);

The C++ Compiler causes the following error :
error C2664: 'Add' : cannot convert parameter 2 from 'void (void)' to 'void (__cdecl *)(void)'

Who can help ?
I think it's a basic C problem, but i can't find out why it's incorrect.

Thanks for your help.

Gomez Addams
April 16th, 1999, 11:47 AM
It's a basic C++ problem. The prototype you specified was a pointer
to a function returning void and taking no arguments. You passed a
pointer to a member function which takes an implicit this pointer and
thus, gave the error. To do this, you need to pass a static member
function, which will have to no this, or a non-member function.
Another possibility is to pass a static member function along with an
object pointer if you need one.

Paul McKenzie
April 16th, 1999, 11:50 AM
I think the problem is that you are assigning a member function pointer to a global function pointer. In C++, you can't do this.

The function type of CMyProject::GetDate() is not void (*Func)() but void (CMyProject::*Func)(). You need to do the following (I use typedefs since
function pointers tire me out):

typedef void (CMyProject::*FUNCPTR)();

typedef struct {
...
FUNCPTR Func;
} ITEM, *pItem

void CMyProject::Add( char *szItemName, FUNCPTR Func)
{
// Add a new data type
pITEM pData = new ITEM();
strcpy(pData->szTypeName,szItemName);
pData->Func = Func;
m_aDataType.Add(pData);
}

The call is invoked by doing this:
(pData->*Func)();

This is tough syntax to remember, but the bottom line is that in C++ non-static member functions have a hidden extra parameter ("this"), so you cannot declare it the same way as a global / static function pointer.

Regards,

Paul McKenzie

flotat
April 16th, 1999, 12:28 PM
Thanks i don't have any more compile error, but could you help me with the following incorrect code :

I declared the function pointer like you told me but the following code
to execute the function for a specified found item in the list does not compile :

for (int i=0; i < m_aDataType.GetSize(); i++)
{
if (FindItem( m_aDataType.GetAt(i)->szTypeName))
{
// Execute appropriate function
pItem pData=m_aDataType.GetAt(i); --- Incorrect i think
(pData->*Func)(); --- Func is declared as unknown at compile time
}
}

Many Thanks for your help.

Paul McKenzie
April 16th, 1999, 02:32 PM
Unless my syntax is wrong, Func should be a member of the ITEM struct. What does the definition of ITEM look like?

Also, I noticed by looking at some of the code, it seems that using "virtual" would have been a better choice than declaring a function pointer.

Regards,

Paul McKenzie