简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
1.前言
本篇目的:C++之普通函数指针和类成员函数指针总结
2.应用实例
#include <iostream>
#include <string>
#include <stdio.h>
#include <functional>
using namespace std;
//一、普通函数回调
//1.返回类型:int; 参数类型(int, int).
int get_data(int x, int y){
return x+y;
}
//v1.0
int (*callback)(int x, int y);
//v2.0 typedef
typedef int (*TCallBack)(int x, int y);
//v3.0 using
using UCall = int (*)(int x, int y);
//二、类成员函数回调
class ICameraService{
public:
//1.返回类型:int; 参数类型(int, string buf).
int test(int a, string buf){
printf("xxx----------%s(), line = %d, a = %d, buf = %s\n",__FUNCTION__,__LINE__,a,buf.c_str());
return a;
}
};
//三、lambda表达式函数回调
int lambda_test(std::function<void(const std::string& desc)> _hidl_cb){
_hidl_cb("AAAAAAAAAAAAAAAAAAAAAAAA");
return 12;
}
int main(void){
//一、普通函数回调
//v1.0
callback = get_data;
printf("line = %d, sum = %d\n", __LINE__,callback(1, 3));
//v2.0 typedef:定义普通函数指针
TCallBack tcall;
tcall = get_data;
printf("line = %d, sum = %d\n", __LINE__,tcall(10, 30));
//v3.0 using:定义普通函数指针
UCall ucall = get_data;
printf("line = %d, sum = %d\n", __LINE__,ucall(10, 300));
//v4.0 function + bind:定义普通函数指针
function<int (int, int)> ft01 = bind(get_data, std::placeholders::_1, std::placeholders::_2);
printf("line = %d, sum = %d\n", __LINE__,ft01(12, 400));
//v5.0 atuo + bind:定义普通函数指针
auto ft02 = bind(get_data, std::placeholders::_1, std::placeholders::_2);
printf("line = %d, sum = %d\n", __LINE__,ft02(13, 500));
printf("----------------------------------------------------------------\n\n");
//二、ICameraService类成员函数回调
//v1.0 typedef:定义类成员函数指针
//定义ICameraService类的成员函数的类型TCamCallback,即函数指针的类型为TCamCallback.
typedef int (ICameraService::*TCamCallback)(int a, string buf);
TCamCallback tcb;//定义成员函数指针
//设置回调函数初始化,将成员函数test的地址赋值给函数指针tcb
tcb = &ICameraService::test;
ICameraService cam01;//定义ICameraService对象cam01
(cam01.*tcb)(10, "1111111");
//或者
//ICameraService *cam01 = new ICameraService;//定义ICameraService对象cam01
//(cam01->*tcb)(10, "1111111");
//v2.0 using:定义类成员函数指针
using UCamCallback = int (ICameraService::*)(int a, string buf);
UCamCallback ucb;
ucb = &ICameraService::test;
ICameraService cam02;
(cam02.*ucb)(11, "2222222");
//v3.0 function + bind:定义类成员函数指针
ICameraService cam03;
function<int (int, string)> fb01 = bind(&ICameraService::test, &cam03, std::placeholders::_1, std::placeholders::_2);
fb01(12, "3333333");
//v4.0 auto + bind:定义类成员函数指针
ICameraService cam04;
auto at01 = bind<int>(&ICameraService::test, &cam04, std::placeholders::_1, std::placeholders::_2);
at01(13,"4444444");
printf("----------------------------------------------------------------\n\n");
//三、lambda表达式函数回调
//v1.0 直接使用:function + lambda:定义普通函数指针
int aa = 10;
function<int (int, int)> ft03 =[&aa](int a, int b) -> int {
return a + b + aa;
};
printf("line = %d, sum = %d\n", __LINE__,ft03(10,10));
//v2.0 直接使用:auto + lambda:定义普通函数指针
int bb = 100;
auto ft04 =[&bb](int a, int b) -> int {
return a + b + bb;
};
printf("line = %d, sum = %d\n", __LINE__,ft04(20,20));
//v3.0 传参的方式:lambda直接回调
int cc = 1000;
lambda_test([&cc](string buf)->int{
printf("line = %d, buf = %s\n", __LINE__,buf.c_str());
return cc;
});
return 0;
}