boost::function的简单用法
2014-06-26 17:04
429 查看
#include <iostream>
#include "boost/function.hpp"
#include "boost/bind.hpp"
using namespace std;
class Foo
{
public:
void methodA()
{
cout<<" Foo.methodA"<<endl;
}
void methodInt(int a)
{
cout<<" methodInt: a = "<<a<<endl;
}
};
class Bar
{
public:
void methodB()
{
cout<<" Bar.methodB"<<endl;
}
};
void fun1( int a )
{
cout<<" fun1 : a = "<<a<<endl;
}
void main()
{
boost::function<void()> f1; // 无参数,无返回值
Foo foo;
f1 = boost::bind(&Foo::methodA, &foo);
f1(); // 调用 foo.methodA();
f1 = boost::bind(&Foo::methodInt, &foo, 42);
f1(); // 调用 foo.methodInt(42);
Bar bar;
f1 = boost::bind(&Bar::methodB, &bar);
f1(); // 调用 bar.methodB();
boost::function<void(int)> f2; // int 参数,无返回值
f2 = boost::bind(&Foo::methodInt, &foo, _1);
f2(53); // 调用 foo.methodInt(53);
boost::function<void(int)> myFun(&fun1); //对于普通函数
myFun(12);
system("pause");
}
#include "boost/function.hpp"
#include "boost/bind.hpp"
using namespace std;
class Foo
{
public:
void methodA()
{
cout<<" Foo.methodA"<<endl;
}
void methodInt(int a)
{
cout<<" methodInt: a = "<<a<<endl;
}
};
class Bar
{
public:
void methodB()
{
cout<<" Bar.methodB"<<endl;
}
};
void fun1( int a )
{
cout<<" fun1 : a = "<<a<<endl;
}
void main()
{
boost::function<void()> f1; // 无参数,无返回值
Foo foo;
f1 = boost::bind(&Foo::methodA, &foo);
f1(); // 调用 foo.methodA();
f1 = boost::bind(&Foo::methodInt, &foo, 42);
f1(); // 调用 foo.methodInt(42);
Bar bar;
f1 = boost::bind(&Bar::methodB, &bar);
f1(); // 调用 bar.methodB();
boost::function<void(int)> f2; // int 参数,无返回值
f2 = boost::bind(&Foo::methodInt, &foo, _1);
f2(53); // 调用 foo.methodInt(53);
boost::function<void(int)> myFun(&fun1); //对于普通函数
myFun(12);
system("pause");
}
相关文章推荐
- boost之function的简单使用
- boost::regex简单用法
- boost锁的简单用法
- boost bind及function的简单实现
- boost bind function用法说明
- boost::signals的2个最简单用法
- boost::function用法详解
- boost bind及function的简单实现
- boost::function用法详解
- boost::function 用法简介
- boost::regex简单用法
- boost中function的简单应用(将function作为参数进行传递)
- boost::function用法详解
- boost::function用法详解
- boost::function用法详解
- boost之function的简单使用
- Boost.Function 用法
- boost多线程简单用法
- boost::bind 和 boost::function 基本用法
- boost::function用法详解