tr1::function对象作回调函数技术总结
2014-08-14 22:14
253 查看
之前写过一篇文章用于讨论如何让C++类内函数作成员函数,详情见http://blog.csdn.net/this_capslock/article/details/17001003
今天又发现了一个更好的方法,那就是使用C++的TR1中中包含一个function模板类和bind模板函数。使用它们可以实现类似函数指针的功能,但是比函数指针更加灵活。
对于tr1::function对象可以这么理解:它能接受任何可调用物,只要可调用物的的签名式兼容于需求端即可,比如函数指针,仿函数对象,成员函数指针,
例子如下:
函数InterfaceFunc第三个参数是一个FUNC对象,当作回调函数使用
下面是四种常见类型的“可调用物”:
具体调用的时候是这样的:
测试结果一切正常,如下图:
今天又发现了一个更好的方法,那就是使用C++的TR1中中包含一个function模板类和bind模板函数。使用它们可以实现类似函数指针的功能,但是比函数指针更加灵活。
对于tr1::function对象可以这么理解:它能接受任何可调用物,只要可调用物的的签名式兼容于需求端即可,比如函数指针,仿函数对象,成员函数指针,
例子如下:
#include <iostream> #include <functional>//为了使用std::tr1::function #include <string> #include <sstream> using namespace std; typedef tr1::function< int (const string&) > FUNC; void InterfaceFunc( const string& a , const string& b , FUNC f ) {//测试用接口函数,将a+b得到的字符串转成整数并打印出来,f是回调函数 cout << f(a+b) << endl; }先自定义了一种function类型FUNC,它接受所有参数为const string&并且返回值是 int的签名对象。
函数InterfaceFunc第三个参数是一个FUNC对象,当作回调函数使用
下面是四种常见类型的“可调用物”:
int f1( const string& str ) {//正常函数 cout << "int f1( const string& str )" << endl; stringstream ss; ss << str; int result; ss >> result; return result; } class F2 { public: int operator()( const string& str ) {//仿函数 cout << "int F2::operator()( const string& str )" << endl; stringstream ss; ss << str; int result; ss >> result; return result; } }; class F3 { public: int f3( const string& str ) {//类内非静态成员函数 cout << "int F3::f3( const string& str )" << endl; stringstream ss; ss << str; int result; ss >> result; return result; } }; class F4 { public: static int f4( const string& str ) {//类内静态成员函数 cout << "static int F4::f4( const string& str )" << endl; stringstream ss; ss << str; int result; ss >> result; return result; } };这些函数都具有形如int (const string& )形式的签名式,所以都可以被FUNC对象接受。
具体调用的时候是这样的:
int main() { string a = "123"; string b = "456"; //FUNC接受正常函数指针 InterfaceFunc( a , b , f1 ); cout << endl; //FUNC接受仿函数 InterfaceFunc( a , b , F2() ); cout << endl; //FUNC接受类内非静态成员函数 F3 f; InterfaceFunc( a , b , tr1::bind( &F3::f3 , &f , tr1::placeholders::_1 ) ); cout << endl; //FUNC接受类内静态成员函数 InterfaceFunc( a , b , F4::f4 ); system("pause"); }这里需要特别注意下,第三次让FUNC接受类内非静态成员函数时,使用了tr1::bind( &F3::f3 , &f , tr1::placeholders::_1 )这样东西作为参数,它的含义是:让&f作为F3::f3函数中的第1个参数,因为对于类内非静态成员函数,它有一个隐含的第一参数:this指针,因为成员函数是存储位置是在对象之外的,只根据F3::f3的地址无法得知具体对哪个对象操作,tr1::bind的作用正是告诉F3::f3,它隐含的this参数指向的是f这个已经定义好的对象,剩下的事情就和其它情况一样了。
测试结果一切正常,如下图:
相关文章推荐
- 回调函数到对象方法Thunk技术(转载自http://blog.csdn.net/wr960204/archive/2008/01/29/2071480.aspx)
- Symbian编程总结-了解篇-活动对象正解(4)-异步函数的同步调用
- JavaScript中的Function(函数)对象
- 面试题总结 - C 语言 - 函数(function)
- 【C++】对象作为函数参数【原创技术】
- 技术(7)——让函数根据一个以上的对象类型来决定如何虚化
- 关于输入流对象cin的get函数——测试结论总结
- 一起谈.NET技术,总结字符串比较函数
- STL运用的C++技术(6)——函数对象
- 国航OA项目技术总结(二)关于JVM虚拟机中对象的创建,手机端提速的重要优化
- C++中 函数,函数模板,函数对象,函数对象模板 与 回调机制 不得不说的事
- Javascript Function对象扩展之延时执行函数
- Javascript中函数调用与this所指对象关系进行了一下总结
- C++中的函数指针和函数对象总结
- javascript 扩展Function对象 获取函数名称
- Symbian编程总结-基础篇-活动对象正解(4)-异步函数的同步调用
- javascript Function对象的length属性及js函数的预执行
- C++中的函数指针和函数对象总结
- Symbian编程总结-基础篇-活动对象正解(4)-异步函数的同步调用
- setFontColor=new Function("x","document.fgColor=x");函数对象,相当于