1、为何要使用匿名函数lamdba函数
首先,lambda表达式可使代码变得简单,C++中,一个lambda表达式表示一个可调用的代码单元。如代码:ui
#include <QCoreApplication> #include <QDebug> #include <QSharedPointer> int func2(int i) { i += 2; return i+4; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); auto func1 = [](int i) {i += 2; return i+4; }; qDebug()<< "func2: " << func2(6) << '\n'; qDebug()<< "func1: " << func1(6) << '\n'; return a.exec(); }
如上,对于一个简单的计算,咱们能够经过定义函数的形式,或者使用匿名函数的方法来实现。固然,若是这个函数功能十分简单,不会被重复使用,又不至于单独写一个函数,能够经过匿名函数简化代码:this
int result = 0; bool isAdd = false; result = isAdd? [](int a,int b){return a+b;}(10,5):[](int a,int b){return a-b;}(10,5); qDebug()<<result<<endl;
如上能够简单的示例简化的做用。spa
2、匿名函数中于外部的交互指针
int n = 2; qDebug()<<[n](int n1){return n1+n;}(3)<<endl;
如上,方括号[]用来传递局部变量,可在[]内枚举函数体内须要用到的局部变量。使用[=]则外部的全部变量函数体内均可使用。同理[&]用引用的方法也相同,可是若是须要引用的变量是动态的,例如指针,则这种方法可能会引起出错,内存释放不及时,致使其余变量的值出错。[this]这也相似,都能使用函数体外部的变量。blog
若是想要修改操做外面的变量:内存
int n = 2; qDebug()<<[&](int n1){n = 3;return n1+n;}(3)<<endl; //6 qDebug()<<n<<endl; //3
注意,[] 里面获取n和经过=获取全部的变量都只是只读的,不能修改!!!若是只是想要内部修改,不改变局部变量实际值的话,能够经过关键字 mutable实现:get
int n = 2; qDebug()<<[=](int n1)mutable {n = 3;return n1+n;}(3)<<endl; //6 qDebug()<<n<<endl; //2
3、匿名函数在connect中的使用io
事实上,匿名函数做为一个槽函数是很是方便的。table
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); int n = 0; QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout,this, [=]()mutable {n += 1;qDebug()<<n<<endl;}); timer->start(1000); }
注意,lamda匿名函数对应的信号必须是函数指针的形式。
注意:经过mutable的使用,若是是类中的变量的话,是能够修改的,若是是局部变量是修改不了的,代码示例:
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); n = 0; QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout,this, [=]()mutable {n += 1; if(n == 3){timer->stop();}qDebug()<<n<<endl;}); timer->start(1000); } void Widget::on_pushButton_clicked() { qDebug()<<n<<endl; }
如上,我对于定时器跳到n为3的时候就停下来,而后按按钮,查看当前的n的值,发现类中属性n为3。
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); n = 0; qDebug()<<[=](int n1)mutable {n += 3;return n1+n;}(3)<<endl; //6 qDebug()<<n<<endl; //3 }
若是n为局部变量的话:
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); int n = 0; qDebug()<<[=](int n1)mutable {n += 3;return n1+n;}(3)<<endl; //6 qDebug()<<n<<endl; //0 }