Oscar Garcia Lorenz Oscar Garcia Lorenz - 3 months ago 16
C++ Question

C++ Assigning function pointer to another

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


invalid use of member function (did you forget the '()' ?)


┬┐Where is my error?

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

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

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

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