-. - operator(). -. . , , , . , , - , , , .
. , , , :
, - . :
void test1() {
int a = 5;
QObject b;
QObject:connect(&b, &QObject::destroyed, [&a]{ qDebug() << a; });
}
:
int main(int argc, char **argv) {
QObject * focusObject = {};
QApplication app(argc, argv);
QObject * connect(&app, &QGuiApplication::focusObjectChanged,
[&](QObject * obj){ focusObject = obj; });
return app.exec();
}
. :
void MainWindow::onButtonClicked() {
bool foo = {};
QTimer::singleShot(1000, this, [this, foo]{ qDebug() << foo; });
}
- (this) singleShot. , this . , this.
, , undefined , . :
void MainWindow::onButtonClicked()
{
auto t = new QTimer(this);
bool foo = {};
t->setSingleShot(true);
t->setInterval(1000);
t->start();
connect(t, &QTimer::timeout, [=](){
qDebug() << foo;
t->deleteLater();
});
}
t foo -. - :
class $OpaqueType {
QTimer * const t;
bool const foo;
public:
$OpaqueType(QTimer * t, bool foo) :
t(t), foo(foo) {}
void operator()() {
qDebug() << foo;
t->deleteLater();
}
};
void MainWindow::onButtonClicked() {
connect(t, &QTimer::timeout, $OpaqueType(t, foo));
}
lambda - , , , , :
auto f = [&]{ };
connect(o, &Class::signal1, this, f);
connect(p, &Class::signal2, this, f);
- , , . - . decltype. decltype , . C++ , , . .