std::function

绑定普通函数
1 void func(void)
2 {
3     std::cout << __FUNCTION__ << std::endl;
4 }
5 std::function<void(void)> fr1 = func; 
6 fr1();
View Code

绑定类的静态成员函数

 1 class Foo
 2 {
 3 public:
 4     static int foo_func(int a)
 5     {
 6         std::cout << __FUNCTION__ << "(" << a << ") ->: ";
 7         return a;
 8     }
 9 };
10 std::function<int(int)> fr2 = Foo::foo_func; 
11 std::cout << fr2(100) << std::endl; 
View Code

绑定仿函数

 1 class Bar
 2 {
 3 public:
 4     int operator() (int a)
 5     {
 6         std::cout << __FUNCTION__ << "(" << a << ") ->: ";
 7         return a;
 8     }
 9 };
10 Bar bar;
11 fr2 = bar;
12 std::cout << fr2(200) << std::endl;
View Code

猜你喜欢

转载自www.cnblogs.com/osbreak/p/9210152.html