C ++ Assigning a function pointer to another

I want to make a class that stores function pointers, but when I want to store them in member variables, I get this error:

misuse of member function (did you forget "()"?)

ΒΏWhere is my mistake?

class Button { public: Button(PS3USB * ps3, ButtonEnum button, void (*onPress)(void) = nullptr, void (*onRelease)(void) = nullptr) { PS3 = ps3; status = false; ERROR ---> onPressFunction = onPress; <--- ERROR ERROR ---> onReleaseFunction = onRelease; <--- ERROR id = button; } void check() { if (PS3->getButtonClick(id) && !status) { if (onPressFunction != nullptr) { onPressFunction(); } status = !status; } else if (!PS3->getButtonClick(id) && status) { if (onReleaseFunction != nullptr) { onReleaseFunction(); } status = !status; } } private: bool status; PS3USB * PS3; ButtonEnum id; void * onPressFunction(void); void * onReleaseFunction(void); }; 

thanks

+5
source share
2 answers
 void * onPressFunction(void); void * onReleaseFunction(void); 

These are declarations of member functions, not a pointer to a function. To declare pointers for use, use:

 void (*onPressFunction)(void); void (*onReleaseFunction)(void); 
+10
source
 void * onPressFunction(void); void * onReleaseFunction(void); 

These above do not declare a pointer to a function, but they create a member function, each of which returns a pointer to a void, I think you meant this:

 void (* onPressFunction)(void); void (* onReleaseFunction)(void); 

Also, for function pointers, I would recommend using typedefs , or std::function

Typedef example

 typedef void(*onReleaseFunction)(void); 

And it can be used as follows:

 onReleaseFunction func = &functionname; 
+2
source

All Articles