虚函数不能作为事件处理函数,取地址时只会得到偏移量而不是函数地址,导致触发事件时跑飞

This commit is contained in:
大石头X2 2017-03-01 00:30:54 +08:00
parent a259dafbca
commit 29202abaa7
2 changed files with 8 additions and 2 deletions

View File

@ -22,7 +22,7 @@ void Button::Set(Pin key, Pin led, Pin relay)
void Button::Set(Pin key, Pin led, bool ledInvert, Pin relay, bool relayInvert)
{
Key.Set(key);
Key.Press.Bind(&Button::OnPress, this);
Key.Press.Bind(&Button::OnKeyPress, this);
Key.UsePress();
Key.Open();
@ -49,6 +49,11 @@ void Button::OnPress(InputPort& port, bool down)
}
}
void Button::OnKeyPress(Button& btn, InputPort& port, bool down)
{
btn.OnPress(port, down);
}
bool Button::GetValue() { return _Value; }
int Button::OnWrite(byte data)

View File

@ -36,10 +36,11 @@ public:
String ToString() const;
private:
protected:
bool _Value; // 状态
virtual void OnPress(InputPort& port, bool down);
static void OnKeyPress(Button& btn, InputPort& port, bool down);
};
#endif