您的位置:首页 > 其它

一个简单的函数适配器的例子

2015-08-27 23:00 190 查看
函数适配器可以实现一种函数的接口转换成另一种函数接口,代码如下所示:

[code]//start from the very beginning,and to create greatness
//@author: Chuangwei Lin
//@E-mail:979951191@qq.com
//@brief: 一个简单的函数适配器的例子(将一个函数接口转变为另一个接口)
#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
using namespace std;
//声明类
class lcw
{
public:
    void memberFunc(double d, int i, int j)//简单的打印函数
    {
        cout << d <<" "<< i << " " << j << endl; 
    }
};
int main()
{
    lcw lin;//声明类
    //相当于是void fp1(int, int),_1是占位符,所占位表示输入的参数,成员函数的话取地址符不能省略
    boost::function<void (int, int)> fp1 = boost::bind(&lcw::memberFunc, &lin, 0.5, _1, _2);
    fp1(120, 220);
    //boost::ref说明此处是一个引用,上面那一条是一个指针
    boost::function<void (int, int)> fp2 = boost::bind(&lcw::memberFunc, boost::ref(lin), 0.5, _1, _2);
    fp2(5 , 26);
    return 0;
}


运行结果如下:

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