boost/function,boost/bind

#include<iostream>
#include<boost/function.hpp>
#include<boost/bind.hpp>
using namespace std;
class Foo
{
public:
    void memberFunc(double d, int i, int j)
    {
        cout << d << endl;
        cout << i << endl;
        cout << j << endl;
    }
void memberFunc2()
{
cout<<" memberFunc2 "<<endl;
} };
void function() { cout << "function" << endl; } void function2(int x) { cout << "function2 " << x << endl; } int main() { //boost::bind的作用是将某个拥有n的参数的函数,变为拥有n-i个参数的函数 //boost::function的作用是声明一个函数的类型, <void(int)>表示此函数返回值是void, 参数是int Foo foo; boost::function<void(int)>fp = boost::bind(&Foo::memberFunc, &foo, 0.5, _1, 10);//成员函数必须有取地址符号,成员函数的第一个参数隐含一个this指针 fp(100); boost::function<void()>fp2 = function; fp2(); boost::function<void()>fp3 = boost::bind(function2, 5);//非成员函数不需要有取地址符号。 fp3();
boost::function<void()>fp4 = boost::bind(&Foo::memberFunc2, &foo) //成员函数即使是无参数的函数,也需要bind一下,因为成员函数隐含有一个this指针的参数
fp4();
return 0; }

 

posted @ 2021-10-06 15:20  Toretto瑞  阅读(32)  评论(0编辑  收藏  举报