Home > Net >  Function pointer to class method as argument
Function pointer to class method as argument

Time:10-26

As you can see by my code, I'm trying to create a onClick event for a button that invoke a function from another class (I'm trying to make my custom button class instead of using win32 default ones for testing).

But even if this does not throw any error, it just doesn't invoke the given method.

This is the function signature inside Button.hpp

void onClick(POINT pt, void (ButtonsHandler::*clicked)(void));

This is the implementation inside Button.cpp (Using clicked() throws C expression preceding parentheses of apparent call must have pointer-to function type)

void Button::onClick(POINT pt, void (ButtonsHandler::*clicked)(void)) 
{
    if (PtInRect(&textRect, pt) != 0) 
    {
        clicked;
    }
}

And this is where I actually call it

mainMenu.getPlay().onClick(pt, &ButtonsHandler::onPlay);

EDIT: solved thank you guys I was just not creating a ButtonsHandler object to execute the non-static function Here's the correct code I was missing.

void Button::onClick(POINT pt, void (ButtonsHandler::*clicked)(void)) {
    if (PtInRect(&textRect, pt) != 0) {
        ButtonsHandler bh;
        (bh.*clicked)();
    }
}

CodePudding user response:

It just doesn't invoke the given method!

The passed pointer to member function has to be called to get in effect.

I assume that the Button is inherited from the ButtonsHandler. Then, for instance, you can call with the pointer to member function with this pointer as follows:

void Button::onClick(POINT pt, void (ButtonsHandler::*clicked)(void)) 
{
    if (PtInRect(&textRect, pt) != 0) {
        (this->*clicked)();
        //^^^^^^^^^^^^^^^^^^
        // or
        // std::invoke(clicked, this) // need to include <functional> header(Since C  17)
    }
}

If both classes are unrelated, you required an instance of ButtonsHandler to call the member function. For example:

ButtonsHandler obj;
(obj.*clicked)();
// or
// std::invoke(clicked, obj) // need to include <functional> header
  • Related