//普通函数重载绑定
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);
时间: 2024-12-17 09:46:07