您的位置:首页 > 编程语言 > C语言/C++

tr1::function对象作回调函数技术总结

2014-08-14 22:14 253 查看
之前写过一篇文章用于讨论如何让C++类内函数作成员函数,详情见http://blog.csdn.net/this_capslock/article/details/17001003

今天又发现了一个更好的方法,那就是使用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这个已经定义好的对象,剩下的事情就和其它情况一样了。

测试结果一切正常,如下图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 回调函数