|
-
April 16th, 1999, 11:23 AM
#1
Function Pointer
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.
-
April 16th, 1999, 11:47 AM
#2
Re: Function Pointer
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.
-
April 16th, 1999, 11:50 AM
#3
Re: Function Pointer
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
-
April 16th, 1999, 12:28 PM
#4
Re: Function Pointer
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.
-
April 16th, 1999, 02:32 PM
#5
Re: Function Pointer
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|