Jeffrey Spoon wrote:
...
Hello, I am trying to make a simple function that returns a pointer to
another function. In my header file I've declared my function pointer:
void (*pStateFunction) (void); //assume the function pointed to returns
void
and the actual function that returns the pointer:
void* getState(CString myString);
My question is, when returning a function pointer to a function that is
known to be void, is there a more sensible way to do it than define the
return value as a pointer to void, i.e. a pointer to an unknown type?
Yes, there is. Moreover what you do now is incorrect. Data pointers (like
'void*') and function pointers (like your 'void (*)(void)') are not compatible
with each other. Forcing a conversion between the two leads to undefined behavior.
If you want your function to return a pointer to a function, you just declare it
as such. The "flat" declaration for 'getState' might look as follows
void (*getState(CString myString))(void);
but this is rather difficult to read. A typedef will definitely help in this
case. You can do either
typedef void VoidFunc(void);
VoidFunc* getState(CString myString);
or
typedef void (*VoidFuncPtr)(void);
VoidFuncPtr getState(CString myString);
--
Best regards,
Andrey Tarasevich