我的c++不如我想的那么好,我更喜欢c#。所以,请告诉我我做错了什么以及如何解决它?
// I've declared base class and event handler for it.
// Some sort of delegate simulation
class IEventable {};
typedef void (*EventHandler)(IEventable*);
//Used this event in class I wand to call back to invoker
class Timer : IEventable {
private:
EventHandler _onTimer;
...
void OnEvent() {
...
if (_onTimer) _onTimer(this);
}
public:
...
void Start(uint32_t interval, EventHandler onTimer) {
...
}
}
// Holder of timer
class Invoker {
private:
// I've put it here for now from timers creator method
Timer* _timer = new Timer();
void _onOpenTimer(Timer timer) {
...
}
void _open() {
...
// And here I have compilation error about wrong use of
// non static member 'void Invoker::_onOpenTimer(Timer)'
_timer.Start(500, _onOpenTimer);
}
}
我有多个计时器,想要确定哪个正在回调。
谢谢。
嗯,我做了一个静态的解决方案。不知道效果如何,但是…
// I've declared base class and event handler for it.
// Some sort of delegate simulation
class IEventable {};
class IEventer {};
typedef void (*EventHandler)(IEventable*, IEventer*);
//Used this event in class I wand to call back to invoker
class Timer : public IEventable {
private:
IEventer* _eventer;
EventHandler _onTimer;
...
void OnEvent() {
...
if (_onTimer) _onTimer(this, eventer);
}
public:
...
void Start(uint32_t interval, IEventer* eventer, EventHandler onTimer) {
_eventer = eventer;
...
}
}
// Timer parent
class Invoker : public IEventer {
private:
// I've put it here for now from timers creator method
Timer* _timer = new Timer();
static void _onOpenTimer(IEventable* sender, IEventer* enenter) {
eventer->OnOpenTimer(sender);
}
void _open() {
...
_timer.Start(500, this, _onOpenTimer);
}
public:
void OnOpenTimer(Timer* sender) {
...
}
};