std::bind 重载绑定

//普通函数重载绑定
void print1()
{
std::cout << "non args \n";
}

void print1(int v)
{
std::cout << "arg value is " << v << std::endl;
}

std::function<void()> fn = std::bind((void (*)())print1);
fn();

std::function<void(int v)> fn1 = std::bind((void(*)(int v))print1, std::placeholders::_1);
fn1(10);


//成员函数重载绑定
class A
{
public:
void print1()
{
std::cout << "mem fn: --- non args\n";
}

void print1(int v)
{
std::cout << "mem fn: --- arg value is " << v << std::endl;
}
};

A a;
std::function<void (A*)> mem_fn = std::bind((void(A::*)())&A::print1, std::placeholders::_1);
mem_fn(&a);

std::function<void (A*, int v)> mem_fn1 = std::bind((void(A::*)(int v))&A::print1, std::placeholders::_1, std::placeholders::_2);
mem_fn1(&a, 10);

posted @ 2015-07-13 21:20  燎原火  阅读(1587)  评论(0编辑  收藏  举报