mercredi 31 juillet 2019

Qt Connections: Using Lambda Expression Resets Passed int Variable (Weird)

Consider the following code:

int counter = 0;

QTimer* timer = new QTimer(this);

connect(timer, &QTimer::timeout, [this, &counter]() mutable {
    counter++;
    qDebug() << counter;
});

timer->start();

Expected:

1
2
3
4
...

Output:

32766 (a random number)
...

Is there some undefined stuff going on here? I can't find anything about this effect.

Aucun commentaire:

Enregistrer un commentaire