我有简单的 UI 类,我需要将 UI 元素实例发送到每个元素的回调,所以我可以(很像在 javascript 中)操作调用回调的元素。
这需要将实例发送this
到注册为回调的函数。我怎么做?
当前状态:
class Opencv_Button {
private:
bool (*callback)(void*); //Callback WITHOUT the current button isntance
//bool(*callback)(Opencv_Button, void*); //this is CALLBACK I WANT instead
void* callback_param; //parameter set by user
bool state;
bool mouse(int, int, int);
public:
Opencv_Button(int, int, int, int);
void setCallback(bool(*)(void*), void*);
//void setCallback(bool(*)(Opencv_Button, void*), void*); //This is what I WANT TO USE
void draw(Mat*, bool);
void val(const char*);
char value[30];
};
现在,有调用回调的函数:
bool Opencv_Button::mouse(int mx, int my, int button)
{
if(/*click conditions satisfied*/)
{
/*CLICK!!!*/
if(callback!=0)
callback(callback_param);
/*Instead I WANT:*/
//callback(*this/*some THIS instance*/, callback_param);
return true;
}
return false;
}
所以,我可以在回调中做这样的事情:
bool buttonCallback(Opencv_Button*but, void*param) {
but->val("I was clicked!!!");
}