What does this code block mean?
Solved
General and Desktop
-
AnalTask *analTask = new AnalTask(); analTask->setOptions(options); MainWindow *main = this->main; connect(analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed); connect(analTask, &AsyncTask::finished, main, [analTask, main]() { if (analTask->getOpenFileFailed()) { return; } main->finalizeOpen(); });
Hi,
I see such code block in a project, and cannot understand the second connect() function, I didn't see such code before. Can someone tell me what does "analTask, main{}" means?Thanks!
-
@Mr-Pang You should read about C++ lambdas: https://en.cppreference.com/w/cpp/language/lambda
connect(analTask, &AsyncTask::finished, main, [analTask, main]() { if (analTask->getOpenFileFailed()) { return; } main->finalizeOpen(); });
In this connect the last parameter is a lambda function. This means it is a function without a function name. In this case this function gets no parameters, but analTask and main are captured and are available inside the lambda function.