0
我正在製作一個用戶界面,兩個主類是UIControl
和UIView
。 兩者都是抽象類。 UI中的每個視圖(例如主菜單等)都具有UIView作爲父類。我希望這些派生視圖類能夠將其自己的成員函數作爲控件的回調。這怎麼能實現?下面多態類的回調函數成員
僞代碼:
class UIControl
{
public:
void (*m_callback_click)(int button_index, int x, int y);
// This class also statically calls callback_click if a control is clicked
}
class UIView
{
// Abstract class
}
class UIMainMenu : public UIView
{
public:
UIControl* m_button1;
void initialize();
void button1_click(int button, int x, int y);
}
void UIMainMenu::initialize()
{
m_button1 = new UIControl();
m_button1->m_callback_click = &button1_click;
}
我建議你閱讀['std :: function'](http://en.cppreference.com/w/cpp/utility/functional/function)和['std :: bind'](http:/ /en.cppreference.com/w/cpp/utility/functional/bind)。 –