我有一個Button類:C++:類具有指針功能屬性
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);
};
而且我要在方法Button::handleEvents
執行Button::function
:
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;
}
}
}
當我嘗試編譯,我得到以下錯誤:
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
當我從類「按鈕」消滅的對象,我應該做的與指針的東西嗎? –
我不明白你的問題。你能澄清一下嗎? – 2012-10-08 07:21:46