函数指针和对象函数
调用方
访问被调用方
的实现函数InvokeFunction被调用方
访问调用方
的回调函数CallbackFunction

被调用模块
使用调用模块
指定的方法完成其功能,比如常见的std::sort比如SDK没有写DebugLog的功能,而是通过回调函数的方式,让调用方实现写DebugLog功能。
通知机制:在一些场景下,
被调用方
通过回调函数去通知调用模块
,去进行相应操作。…
#include <algorithm>#include <iostream>#include <vector>struct Student{std::string m_strName;unsigned int m_uAge;};void PrintStudentVector(const std::vector<Student>& vecStudents){for (auto&& student : vecStudents){std::cout << student.m_strName.c_str() << ":" << student.m_uAge << std::endl;}std::cout << std::endl;}bool StudentSortFunction(const Student& student1, const Student& student2){return student1.m_uAge < student2.m_uAge;}int main(){std::vector<Student> vecStudents= {{"xiaoqiang", 15},{"xiaoming", 13},{"xiaoke", 13}};PrintStudentVector(vecStudents);std::sort(vecStudents.begin(), vecStudents.end(), StudentSortFunction);//Print after sortPrintStudentVector(vecStudents);return 0;}
#include <algorithm>#include <iostream>#include <vector>struct Student{std::string m_strName;unsigned int m_uAge;};void PrintStudentVector(const std::vector<Student>& vecStudents){for (auto&& student : vecStudents){std::cout << student.m_strName.c_str() << ":" << student.m_uAge << std::endl;}std::cout << std::endl;}class StudentSort{public:bool operator() (const Student& student1, const Student& student2){return student1.m_uAge < student2.m_uAge;}};int main(){std::vector<Student> vecStudents= {{"xiaoqiang", 15},{"xiaoming", 13},{"xiaoke", 13}};PrintStudentVector(vecStudents);std::sort(vecStudents.begin(), vecStudents.end(), StudentSort());//Print after sortPrintStudentVector(vecStudents);return 0;}
void CallbackFunction(Contex* pCtx, Parameter par1, Parameter par2.....)
class CallbackContex{public:bool operator() (Parameter par1, Parameter par2) { ; };private:Contex* m_pCtx;};
Lambda
捕获列表,其可以捕获当前上下文的变量,可以是值捕获或者引用捕获
函数参数,不用赘述,和普通函数一样
specifiers, 可选的,主要说明下
mutable
, 默认情况下值捕获,将无法修改其值(可以想象为其成员函数后面跟了个const
),除非设置为mutable
.返回值,如果不写表示返回void
函数体, 这部分可以使用你捕获列表里面的变量,也可以使用参数列表里面的变量。
std::sort(vecStudents.begin(), vecStudents.end(), [](const Student& student1, const Student& student2) -> bool {return student1.m_uAge < student2.m_uAge;});
#include <iostream>int main(){unsigned int uYear = 2020;unsigned int uMonth = 9;std::cout << "uYear: " << uYear<< " Month: " << uMonth << std::endl << std::endl;auto lambda = [&uYear, uMonth]() -> bool {uYear = 2021;std::cout << "uYear: " << uYear<< " Month: " << uMonth << std::endl << std::endl;//error C3491: 'uMonth': a by copy capture cannot be modified in a non-mutable lambda//uMonth = 10;return true;};lambda();std::cout << "uYear: " << uYear<< " Month: " << uMonth << std::endl << std::endl;return 0;}
uYear
是main
函数中的uYear
的引用,对uYear
的重新复制为2021
也会影响到main
中uYearuMonth
只是main
函数中的uMonth
的值传递,默认情况下不能够直接进行改写,除非将Lambda
指定为mutable
。如果其为mutable
, 在函数体内的修改并不会影响main
中uMonth
的改变。
class LambdaClass_XXXXX{public:LambdaClass_XXXXX(unsigned int& uYear, unsigned int uMonth) :m_uYear(uYear), m_uMonth(uMonth) {}bool operator()() const{m_uYear = 2021;std::cout << "uYear: " << m_uYear<< " Month: " << m_uMonth << std::endl << std::endl;return true;}private:unsigned int& m_uYear;unsigned int m_uMonth;};
[&, uMonth]
表示uMonth
采用值捕获
,其他可见的变量均采用`引用捕获[=, &uYear]
表示uYear
采用引用捕获
,其他可见的变量均采用值捕获
给读者的问题
#include <iostream>int main(){int iVal = 100;auto lambda = [iVal]() mutable {iVal += 100;std::cout << iVal << std::endl;};lambda();lambda();return 0;}
总结
文章转载自一个程序员的修炼之路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。






