<pre name="code" class="plain">C++ 接口 回调
1、用抽象类设计接口,包含几个待实现的函数用于事件响应。
如下文class InstrestingEvent,其中包含两个纯虚函数,作为响应。
2、定义一个接口指针,作为被调用类的成员
如在class EventNotifier类中加入InterestingEvent *m_ie。
3、实例化一个接口,作为接口类的子类,其中需要写出具体的响应事件。
如T1,并且将T1的this指针传给Decoder中的m_ie,即定义好了一个可用的接口。
4、等待事件发生,调用接口中的响应事件
如m_Happened2==1,m_ie->interestingevent2();
下面是一个简单的回调栗子:
#include <iostream>
using namespace std;
class InterestingEvent{
public:
virtual int interestingevent1()=0;
virtual i