一、為什么要使用匿名函數lamdba
首先,lambda表達式可以使代碼變得簡單,C++中,一個lambda表達式表示一個可調用的代碼單元。如代碼:
#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();
}
如上,對於一個簡單的計算,我們可以通過定義函數的形式,或者使用匿名函數的方法來實現。當然,如果這個函數功能十分簡單,不會被重復使用,又不至於單獨寫一個函數,可以通過匿名函數簡化代碼:
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;
如上可以簡單的示例簡化的作用。
二、匿名函數中於外部的交互
int n = 2;
qDebug()<<[n](int n1){return n1+n;}(3)<<endl;
如上,方括號[]用來傳遞局部變量,可在[]內枚舉函數體內需要用到的局部變量。使用[=]則外部的所有變量函數體內都可使用。同理[&]用引用的方法也相同,但是如果需要引用的變量是動態的,例如指針,則這種方法可能會引發出錯,內存釋放不及時,導致其他變量的值出錯。[this]這也類似,都能使用函數體外部的變量。
如果想要修改操作外面的變量:
int n = 2;
qDebug()<<[&](int n1){n = 3;return n1+n;}(3)<<endl; //6
qDebug()<<n<<endl; //3
注意,[] 里面獲取n和通過=獲取所有的變量都只是只讀的,不能修改!!!如果只是想要內部修改,不改變局部變量實際值的話,可以通過關鍵字 mutable實現:
int n = 2;
qDebug()<<[=](int n1)mutable {n = 3;return n1+n;}(3)<<endl; //6
qDebug()<<n<<endl; //2
三、匿名函數在connect中的使用
事實上,匿名函數作為一個槽函數是非常方便的。
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
}
