a problem with function pointer
-
Hi fellows I'm developing an app that needs to use function pointer. I'm assigning one of my methods in a function pointer variable. this is my fuction pointer variable:
unsigned int (__stdcall * ObjectEvent)(void*);
This variable needs to be like this because in the header file that I use there are a typedef with the same signature of my variable and some functions of this header that uses this typedef. See:typedef unsigned int (__stdcall *HDSchedulerCallback)(void *pUserData);
This is my method signature:unsigned int __stadcall Object3DS::ObjectCallBack(void * vData) { MessageBox(NULL, "working", "Info", MB_OK); return 1; }
This is my code when I put the method of my class into the function poiner variable:void Object3DS::LoadObject3DS(const char * cObjectName, bool bVisible, float fX, float fY, float fZ) { ... ObjectEvent = ObjectCallBack; m_ObjectHandle = hdScheduleAsynchronous(ObjectEvent, 0, HD_DEFAULT_SCHEDULER_PRIORITY); }
HD_DEFAULT_SCHEDULER_PRIORITY is just a thread priority variable. The error that I receive is this:error C2440: '=' : cannot convert from 'unsigned int (__stdcall Object3DS::*)(void *)' to 'unsigned int (__stdcall *)(void *)'
So fellows what I have to do to solve this?? I've tried somethings but nothing solved. I haven't any idea. -
Hi fellows I'm developing an app that needs to use function pointer. I'm assigning one of my methods in a function pointer variable. this is my fuction pointer variable:
unsigned int (__stdcall * ObjectEvent)(void*);
This variable needs to be like this because in the header file that I use there are a typedef with the same signature of my variable and some functions of this header that uses this typedef. See:typedef unsigned int (__stdcall *HDSchedulerCallback)(void *pUserData);
This is my method signature:unsigned int __stadcall Object3DS::ObjectCallBack(void * vData) { MessageBox(NULL, "working", "Info", MB_OK); return 1; }
This is my code when I put the method of my class into the function poiner variable:void Object3DS::LoadObject3DS(const char * cObjectName, bool bVisible, float fX, float fY, float fZ) { ... ObjectEvent = ObjectCallBack; m_ObjectHandle = hdScheduleAsynchronous(ObjectEvent, 0, HD_DEFAULT_SCHEDULER_PRIORITY); }
HD_DEFAULT_SCHEDULER_PRIORITY is just a thread priority variable. The error that I receive is this:error C2440: '=' : cannot convert from 'unsigned int (__stdcall Object3DS::*)(void *)' to 'unsigned int (__stdcall *)(void *)'
So fellows what I have to do to solve this?? I've tried somethings but nothing solved. I haven't any idea.The problem comes from the fact that global functions and class functions don't have the same signature: for the class functions, the this (address of the instance of the class which call the function) is passed implicitely with the paramters. If you want to solve the problem, specify your function as statis but then the problem will be that you won't be able to access non-static members (the function doesn't belong to any instance). Again, you can solve this problem by using a global function as callback and pass the address of your instance (this pointer) to the
hdScheduleAsynchronous
function. Normally, every call back lets you do this. Then, inside your global callback function, cast the void pointer to a Object3DS* pointer and call a function from within your class. An even nicer way to do it is not to use a global function but a static function from your class.
Cédric Moonen Software developer
Charting control [v1.1] -
Hi fellows I'm developing an app that needs to use function pointer. I'm assigning one of my methods in a function pointer variable. this is my fuction pointer variable:
unsigned int (__stdcall * ObjectEvent)(void*);
This variable needs to be like this because in the header file that I use there are a typedef with the same signature of my variable and some functions of this header that uses this typedef. See:typedef unsigned int (__stdcall *HDSchedulerCallback)(void *pUserData);
This is my method signature:unsigned int __stadcall Object3DS::ObjectCallBack(void * vData) { MessageBox(NULL, "working", "Info", MB_OK); return 1; }
This is my code when I put the method of my class into the function poiner variable:void Object3DS::LoadObject3DS(const char * cObjectName, bool bVisible, float fX, float fY, float fZ) { ... ObjectEvent = ObjectCallBack; m_ObjectHandle = hdScheduleAsynchronous(ObjectEvent, 0, HD_DEFAULT_SCHEDULER_PRIORITY); }
HD_DEFAULT_SCHEDULER_PRIORITY is just a thread priority variable. The error that I receive is this:error C2440: '=' : cannot convert from 'unsigned int (__stdcall Object3DS::*)(void *)' to 'unsigned int (__stdcall *)(void *)'
So fellows what I have to do to solve this?? I've tried somethings but nothing solved. I haven't any idea. -
Hi fellows I'm developing an app that needs to use function pointer. I'm assigning one of my methods in a function pointer variable. this is my fuction pointer variable:
unsigned int (__stdcall * ObjectEvent)(void*);
This variable needs to be like this because in the header file that I use there are a typedef with the same signature of my variable and some functions of this header that uses this typedef. See:typedef unsigned int (__stdcall *HDSchedulerCallback)(void *pUserData);
This is my method signature:unsigned int __stadcall Object3DS::ObjectCallBack(void * vData) { MessageBox(NULL, "working", "Info", MB_OK); return 1; }
This is my code when I put the method of my class into the function poiner variable:void Object3DS::LoadObject3DS(const char * cObjectName, bool bVisible, float fX, float fY, float fZ) { ... ObjectEvent = ObjectCallBack; m_ObjectHandle = hdScheduleAsynchronous(ObjectEvent, 0, HD_DEFAULT_SCHEDULER_PRIORITY); }
HD_DEFAULT_SCHEDULER_PRIORITY is just a thread priority variable. The error that I receive is this:error C2440: '=' : cannot convert from 'unsigned int (__stdcall Object3DS::*)(void *)' to 'unsigned int (__stdcall *)(void *)'
So fellows what I have to do to solve this?? I've tried somethings but nothing solved. I haven't any idea.Alex Cutovoi wrote:
So fellows what I have to do to solve this??
See here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Alex Cutovoi wrote:
So fellows what I have to do to solve this??
See here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
Tks for you all. I hope that works now.
-
You can do that only with a
static
method of the class. But if your method has to access instance members then you must find a way to pass them (perhaps the whole instance of the class) to it.Tks for you all. I hope that works now.
-
The problem comes from the fact that global functions and class functions don't have the same signature: for the class functions, the this (address of the instance of the class which call the function) is passed implicitely with the paramters. If you want to solve the problem, specify your function as statis but then the problem will be that you won't be able to access non-static members (the function doesn't belong to any instance). Again, you can solve this problem by using a global function as callback and pass the address of your instance (this pointer) to the
hdScheduleAsynchronous
function. Normally, every call back lets you do this. Then, inside your global callback function, cast the void pointer to a Object3DS* pointer and call a function from within your class. An even nicer way to do it is not to use a global function but a static function from your class.
Cédric Moonen Software developer
Charting control [v1.1]Tks for you all. I hope that works now.