c++ 回调函数使用
C++ 函数 回调 使用
2023-09-27 14:24:37 时间
普通回调
#include<stdio.h> void printWelcome(int len) { printf("welcome -- %d\n", len); } void printGoodbye(int len) { printf("byebye-- %d\n", len); } void callback(int times, void (* print)(int)) { int i; for (i = 0; i < times; ++i) { print(i); } printf("\n welcome or byebye !\n"); } void main(void) { callback(10, printWelcome); callback(10, printGoodbye); }
类成员函数回调
#include <iostream> #include <functional> using namespace std; using namespace std::placeholders; typedef std::function<void(int,int)> Fun; class B{ public: void call(int a,Fun f) { f(a,2); } }; class Test{ public: void callback(int a,int b) { cout<<a<<"+"<<b<<"="<<a+b<<endl; } void bind() { Fun fun=std::bind(&Test::callback,this,_1,_2); B b; b.call(1,fun); } }; int main() { Test test; test.bind(); return 0; }
bind函数
一般常用语法是: newFunName=bind(oldFunName,arg_list);
bind函数返回一个新的函数对象。其中bind第一个参数是oldFunName,它是待绑定的函数名,arg_list是oldFunName的参数列表。注意,这个参数列表是旧函数的参数列表,前面提到,返回的是子函数。我们可以随便给子函数定几个参数,但是肯定不能多于bind所绑定的原函数的参数个数。举个例子:
//g是一个有两个参数的可调用对象 auto g=bind(f,a,b,_2,c,_1); //其中f是具有5个参数的函数 //当我们调用g(x,y)时,实际调用的是f(a,b,y,c,x)
上面出现的_1,_2是它的占位符,bind最多可以使用9个占位符。这个占位符命名在std的placeholders中,使用时,要使用using std::placeholders.
function函数
function是一个函数对象的“容器”。
如function<int(int,int)> fun; fun是一个函数模板,可以接受两个int型参数,并返回一个int型参数。平时可以将它赋值给一个函数指针。
又一个栗子
#include <iostream> #include <functional> using namespace std; typedef std::function<void ()> fp; void g_fun() { cout<<"g_fun()"<<endl; } class A { public: static void A_fun_static() { cout<<"A_fun_static()"<<endl; } void A_fun() { cout<<"A_fun()"<<endl; } void A_fun_int(int i) { cout<<"A_fun_int() "<<i<<endl; } //非静态类成员,因为含有this指针,所以需要使用bind void init() { fp fp1=std::bind(&A::A_fun,this); fp1(); } void init2() { typedef std::function<void (int)> fpi; //对于参数要使用占位符 std::placeholders::_1 fpi f=std::bind(&A::A_fun_int,this,std::placeholders::_1); f(5); } }; int main() { //绑定到全局函数 fp f2=fp(&g_fun); f2(); //绑定到类静态成员函数 fp f1=fp(&A::A_fun_static); f1(); A().init(); A().init2(); return 0; }
参考于:
http://blog.csdn.net/hyp1977/article/details/51784520
相关文章
- C++ 获取自身函数内存地址附近的字节码
- C++ 获取当前时间
- C/C++ 进程的探索(fork函数)
- C/C++获取当前路径、获取任意pid完整路径(readlink函数)
- C/C++ getopt()函数的介绍及使用
- C++中可以以“类名::成员函数”直接引用成员函数哦!
- C++类中成员函数声明后面的const的含义
- 18 C++ - 函数重载(overload)
- 85 C++ - 函数对象适配器
- 学习C++ -> 类(Classes)的定义与实现
- C++函数的传入参数是指针的指针(**)的详解
- C++ 二维数组
- C++11新式洗牌std::shuffle与老式洗牌函数std::random_shuffle的区别
- 《C++ 黑客编程揭秘与防范(第2版)》——6.6 调试API函数的使用
- 《数字图像处理与机器视觉——Visual C++与Matlab实现(第2版)》——1.2 数字图像处理与机器视觉
- 基于C++实现(控制台)抽象数据类型B树【100010603】
- c++11 跨平台多线程demo和qt 静态链接(std::thread有join函数,设置 QMAKE_LFLAGS = -static)
- 析构函数 (C++)
- c/c++:回调函数
- C++ 函数的使用
- C++STL 仿函数
- C++调用空指针对象的成员函数成功的理解
- 给C++函数形参传入一个C类型的函数指针