2

I have a Button class:

class Button : public Component {

private:
    SDL_Rect box;
    void* function;

public:
    Button( int x, int y, int w, int h, void (*function)() );
    ~Button();
    void handleEvents(SDL_Event event);

};

And I want to execute Button::function in the method Button::handleEvents:

void Button::handleEvents(SDL_Event event) {
    int x = 0, y = 0;
    // If user clicked mouse
    if( event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
            // Get mouse offsets
            x = event.button.x;
            y = event.button.y;

            // If mouse inside button
            if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
            {
                this->function();
                return;
            }
    }

}

When I try to compile, I get the following errors:

Button.cpp: In the constructor ‘Button::Button(int, int, int, int, void (*)(), std::string)’:
Button.cpp:17:18: error: invalid conversion from ‘void (*)()’ to ‘void*’ [-fpermissive]
Button.cpp: In the function ‘virtual void Button::handleEvents(SDL_Event)’:
Button.cpp:45:19: error: can't use ‘((Button*)this)->Button::function’ as a function

4 Answers 4

2

The function pointer in your private section is not declared as it should be.

It should be :

void (*functionPtr)();

Look at this question for more info.

2
  • When I destroy an object from class "Button", should I do something with that pointer? Oct 7, 2012 at 22:08
  • I do not understand your question. Can you please clarify it?
    – user744186
    Oct 8, 2012 at 7:21
2

In your class variable declarations, you have

void* function;

This declares a variable named function which is a pointer to void. To declare it as a function pointer, you need the same syntax as that in your parameter list:

void (*function)();

This is now a pointer to a function which returns void.

1

I suggest std::function<void()> for this kind of purpose:

#include <functional>

class Button : public Component
{
private:
    SDL_Rect box;
    std::function<void()> function;

public:
    Button( int x, int y, int w, int h, std::function<void()> f);
    ~Button();
    void handleEvents(SDL_Event event);
};

void Button::handleEvents(SDL_Event event)
{
    int x = 0, y = 0;
    // If user clicked mouse
    if( event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT)
    {
        // Get mouse offsets
        x = event.button.x;
        y = event.button.y;
        // If mouse inside button
        if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
        {
            function();
            return;
        }
    }
}
0

You might change the field declaration,

void * function; // a void ptr
void (*function)(); // a ptr to function with signature `void ()`

or use a cast:

((void (*)())this->function)();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.