码迷,mamicode.com
首页 > 其他好文 > 详细

std::bind 重载绑定

时间:2015-07-13 22:20:05      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

//普通函数重载绑定
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);

std::bind 重载绑定

标签:

原文地址:http://www.cnblogs.com/and_swordday/p/4643975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!