AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
## 函数指针 C++中函数指针的用途非常广泛,例如函数回调,接口类设计等,但函数指针却不够灵活,如下面进行的测试,对普通函数,lambda表达式,类静态成员函数可以绑定,但对于类的成员函数,仿函数就无能为力了。因此,C++11推出std::function与std::bind这两件大杀器。 ```cpp #include <iostream> #include <memory> typedef void (*Callback)(); void test(std::string s, Callback callback) { std::cout << s << std::endl; callback(); } // 普通函数 void callback() { std::cout << "nomal callback" << std::endl; } class Test { public: static void staticCallback() // 类静态函数 { std::cout << "static callback" << std::endl; } void callback() // 类成员函数 { std::cout << "class inner callback" << std::endl; } }; // 仿函数,重载()运算符 struct Functor { void operator() () { std::cout << "functor callback" << std::endl; } }; int main(int argc, char** argv) { test("test1", callback); test("test2", []() { std::cout << "lambda callback" << std::endl; }); test("test3", Test::staticCallback); // test("test", Functor()); // 仿函数编译报错 // Test t; // test("test5", t.callback); // 类成员函数编译报错 return 0; } ``` 运行结果: ``` test1 nomal callback test2 lambda callback test3 static callback ``` ## std::function C++11后可以用std::function来代替函数指针,可以说std::function是函数指针的超集,它除了具备函数指针的功能,还能指向仿函数,类成员函数甚至函数签名不一致的函数,当然后两种需要和std::bind配合使用。 ```cpp #include <iostream> #include <memory> #include <functional> typedef std::function<void()> Func; void test(std::string s, Func callback) { std::cout << s << std::endl; if (callback) callback(); } // 普通函数 void callback() { std::cout << "nomal callback" << std::endl; } class Test { public: static void staticCallback() // 类静态函数 { std::cout << "static callback" << std::endl; } void callback() // 类成员函数 { std::cout << "class inner callback" << std::endl; } }; // 仿函数,重载()运算符 struct Functor { void operator() () { std::cout << "functor callback" << std::endl; } }; void callback1(int a, int b) // 函数签名不同的函数 { std::cout << "nomal callback a=" << a << ", b=" << b << std::endl; } int main(int argc, char** argv) { test("test1", callback); test("test2", []() { std::cout << "lambda callback" << std::endl; }); test("test3", Test::staticCallback); test("test4", Functor()); // 指向类成员函数 Test t; auto callback5 = std::bind(&Test::callback, t); test("test5", callback5); // 指向函数签名不同的函数 auto callback6 = std::bind(&callback1, 10, 100); test("test6", callback6); return 0; } ``` 运行结果: ``` test1 nomal callback test2 lambda callback test3 static callback test4 functor callback test5 class inner callback test6 nomal callback a=10, b=100 ``` ## 绑定函数传参 ``` #include <iostream> #include <memory> #include <functional> typedef std::function<void(int)> Func; void test(int x, Func callback) { std::cout << "x=" << x << std::endl; if (callback) callback(x); } class Test { public: void callback(int x) // 类成员函数 { std::cout << "class inner callback, x=" << x << std::endl; } }; void callback(int a, int b) // 函数签名不同的函数 { std::cout << "nomal callback a=" << a << ", b=" << b << std::endl; } int main(int argc, char** argv) { // 指向类内成员函数 Test t; auto callback1 = std::bind(&Test::callback, t, std::placeholders::_1); test(10, callback1); // 指向函数签名不同的函数 auto callback2 = std::bind(&callback, std::placeholders::_1, 100); test(11, callback2); return 0; } ``` 运行结果: ``` x=10 class inner callback, x=10 x=11 nomal callback a=11, b=100 ``` 参考:[https://cloud.tencent.com/developer/article/1474565](https://cloud.tencent.com/developer/article/1474565)