boost::bind 与 boost::function 的使用方法例子

简介:

啥也不说,直接看代码:


#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
 
using namespace boost;
using namespace std;
 
int f(int a, int b)
{
    return a + b;
}
 
int g(int a, int b, int c)
{
    return a + b * c;
}
 
class Point
{
public:
    Point(int x, int y) : _x(x), _y(y) {}
    void print(const char *msg) {
        cout << msg << "x=" << _x << ", y=" << _y << endl;
    }
private:
    int _x, _y;
};
 
int main()
{
    //! f 有多少个参数,f 后面就要跟多少个参数。如果不明确的,用占位符
    cout << bind(f, 2, 3)() << endl;    // ==> f(2, 3)
    cout << bind(f, 12, _1) (5) << endl;   // ==> f(12, 5),其中参数b为不明确参数
    cout << bind(g, _2, _1, 3) (3, 5) << endl;  // ==> g(5, 3, 3) 注意顺序
 
    Point p(11, 34);
    Point &rp = p;
    Point *pp = &p;
 
    bind(&Point::print, p, "Point: ") ();
    //   ^              ^
    // 注意,为表示Point::print为成员函数,成员函数前面要加&区分。
    // 对象可以为实例、引用、指针
    bind(&Point::print, rp, "Reference: ") ();  //! 引用
    bind(&Point::print, pp, _1) ("Pointer: ");  //! 指针
    bind(&Point::print, _1, _2) (p, "As parameter: ");  //! 对象也可以用占位符暂时代替
 
    //! function的类型定义与需要的参数有关
    function<void ()> func0 = bind(&Point::print, p, "func0: ");
    function<void (const char *)> func1 = bind(&Point::print, pp, _1);
 
    func0();    //! function对象的调用
    func1("func1: ");
 
    return 0;
}

一般情况下,bind 与 function 配合使用。

bind与function还可以将类型完成不同的函数(成员函数与非成员函数)包装成统一的函数调用接口。如下示例:


#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <vector>
 
using namespace boost;
using namespace std;
 
class Point
{
public:
    Point(int x, int y) : _x(x), _y(y) {}
    void print(string msg) {
        cout << msg << "x=" << _x << ", y=" << _y << endl;
    }
private:
    int _x, _y;
};
 
class Person
{
public:
    Person(string name, int age) : _name(name), _age(age) {}
    void sayHello() {
        cout << "Hello, my name is " << _name << ", my age is : " << _age << endl;
    }
private:
    string _name;
    int _age;
};
 
void printSum(int limit)
{
    int sum = 0;
    for (int i = 0; i < limit; ++i) {
        sum += i;
    }
    cout << "sum = " << sum << endl;
}
 
void doAll(vector<function<void ()> > &funcs)
{
    for (size_t i = 0; i < funcs.size(); ++i) {
        funcs[i] ();
    }
}
 
int main()
{
    vector<function<void ()> > funcList;
 
    Person john("John", 23);
    funcList.push_back(bind(&Person::sayHello, john));
 
    Point p1(23, 19);
    funcList.push_back(bind(&Point::print, p1, "Point: "));
 
    funcList.push_back(bind(printSum, 20));
 
    doAll(funcList);
    return 0;
}

目录
相关文章
|
4月前
|
消息中间件 Kubernetes NoSQL
c++ 11 中 function,lambda,bind
c++ 11 中 function,lambda,bind
|
4月前
|
存储 编译器 C语言
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题(下)
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题
67 5
|
4月前
|
算法 编译器 C语言
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题(中)
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题
44 2
|
4月前
|
算法 编译器 C语言
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题(上)
从C语言到C++_34(C++11_下)可变参数+ lambda+function+bind+笔试题
30 1
|
4月前
|
C++
C++高级开发之可调用对象、function、bind(2)
std::bind 绑定器   要使用这个函数模板,在 cpp文件前面要包含如下头文件#include<funcitonal>   std::bind能够将对象以及相关的参数绑定到一起,绑定完成后可以直接调用,也可以用
51 0
|
4月前
|
存储 算法 对象存储
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
【C++入门到精通】function包装器 | bind() 函数 C++11 [ C++入门 ]
62 1
|
4月前
|
存储 安全 编译器
【C++ 包装器类 std::function 和 函数适配器 std::bind】 C++11 全面的std::function和std::bind的入门使用教程
【C++ 包装器类 std::function 和 函数适配器 std::bind】 C++11 全面的std::function和std::bind的入门使用教程
76 0
|
4月前
|
自然语言处理 Java 编译器
C++中function,bind,lambda
C++中function,bind,lambda
C++中function,bind,lambda
|
4月前
C++11实用技术(二)std::function和bind绑定器
C++11实用技术(二)std::function和bind绑定器
54 0
|
4月前
|
C++
C++高级开发之可调用对象、function、bind(1)
可调用对象   以前函数调用总是离不开一堆圆括号,没错“()”就是函数调用的一个明显标记,这个 “()”有一个称呼叫函数调用运算符。
67 0

热门文章

最新文章