C++中回调(CallBack)的使用方法

转自:http://c.chinaitlab.com/cc/example/200908/791081.html

 

回调函数是一个很有用,也很重要的概念。当发生某种事件时,系统或其他函数将会自动调用你定义的一段函数。回调函数在windows编程使用的场合很多,比如Hook回调函数:MouseProc,GetMsgProc以及EnumWindows,DrawState的回调函数等等,还有很多系统级的回调过程。 一般情况下, 我们使用的回调函数基本都是采用C语言风格。 这里介绍一种C++风格的回调对象方法。 采用template实现。


 view plaincopy to clipboardprint?
template < class Class, typename ReturnType, typename Parameter >
class SingularCallBack
{

public:

typedef ReturnType (Class::*Method)(Parameter);

SingularCallBack(Class* _class_instance, Method _method)
{
//取得对象实例地址,及调用方法地址
class_instance = _class_instance;
method = _method;
};

ReturnType operator()(Parameter parameter)
{
// 调用对象方法
return (class_instance->*method)(parameter);
};

ReturnType execute(Parameter parameter)
{
// 调用对象方法
return operator()(parameter);
};


private:

Class* class_instance;
Method method;

};
template < class Class, typename ReturnType, typename Parameter >
class SingularCallBack
{

public:

typedef ReturnType (Class::*Method)(Parameter);

SingularCallBack(Class* _class_instance, Method _method)
{
//取得对象实例地址,及调用方法地址
class_instance = _class_instance;
method = _method;
};

ReturnType operator()(Parameter parameter)
{
// 调用对象方法
return (class_instance->*method)(parameter);
};

ReturnType execute(Parameter parameter)
{
// 调用对象方法
return operator()(parameter);
};


private:

Class* class_instance;
Method method;

};

    示例:

    以下是两个类实现。

 view plaincopy to clipboardprint?
class A
{

public:

void output()
{
std::cout << "I am class A :D" << std::endl;
};

};

class B
{

public:

bool methodB(A a)
{
a.output();
return true;
}

};
class A
{

public:

void output()
{
std::cout << "I am class A :D" << std::endl;
};

};

class B
{

public:

bool methodB(A a)
{
a.output();
return true;
}

};

    SingularCallBack的各种调用示例:

 view plaincopy to clipboardprint?
A a;
B b;

SingularCallBack< B,bool,A >* cb;
cb = new SingularCallBack< B,bool,A >(&b,&B::methodB);

if((*cb)(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}
A a;
B b;

SingularCallBack< B,bool,A >* cb;
cb = new SingularCallBack< B,bool,A >(&b,&B::methodB);

if((*cb)(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}


view plaincopy to clipboardprint?
A a;
B b;

SingularCallBack< B,bool,A >* cb;
cb = new SingularCallBack< B,bool,A >(&b,&B::methodB);

if(cb->execute(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}
A a;
B b;

SingularCallBack< B,bool,A >* cb;
cb = new SingularCallBack< B,bool,A >(&b,&B::methodB);

if(cb->execute(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}


view plaincopy to clipboardprint?
A a;
B b;
SingularCallBack< B,bool,A >cb(&b,&B::methodB);

if(cb(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}
A a;
B b;
SingularCallBack< B,bool,A >cb(&b,&B::methodB);

if(cb(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}

    view plaincopy to clipboardprint?

 

 class AClass
{
public:

AClass(unsigned int _id): id(_id){};
~AClass(){};

bool AMethod(std::string str)
{
std::cout << "AClass[" << id << "]: " << str << std::endl;
return true;
};

private:

unsigned int id;

};


typedef SingularCallBack < AClass, bool, std::string > ACallBack;


int main()
{

std::vector < ACallBack > callback_list;

AClass a1(1);
AClass a2(2);
AClass a3(3);

callback_list.push_back(ACallBack(&a1, &AClass::AMethod));
callback_list.push_back(ACallBack(&a2, &AClass::AMethod));
callback_list.push_back(ACallBack(&a3, &AClass::AMethod));

for (unsigned int i = 0; i < callback_list.size(); i++)
{
callback_list[i]("abc");
}

for (unsigned int i = 0; i < callback_list.size(); i++)
{
callback_list[i].execute("abc");
}

return true;

}
class AClass
{
public:

AClass(unsigned int _id): id(_id){};
~AClass(){};

bool AMethod(std::string str)
{
std::cout << "AClass[" << id << "]: " << str << std::endl;
return true;
};

private:

unsigned int id;

};


typedef SingularCallBack < AClass, bool, std::string > ACallBack;


int main()
{

std::vector < ACallBack > callback_list;

AClass a1(1);
AClass a2(2);
AClass a3(3);

callback_list.push_back(ACallBack(&a1, &AClass::AMethod));
callback_list.push_back(ACallBack(&a2, &AClass::AMethod));
callback_list.push_back(ACallBack(&a3, &AClass::AMethod));

for (unsigned int i = 0; i < callback_list.size(); i++)
{
callback_list[i]("abc");
}

for (unsigned int i = 0; i < callback_list.size(); i++)
{
callback_list[i].execute("abc");
}

return true;

}

    引用:

    C++ Callback Solution

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值