Why is foreach iterating with a const reference?

后端 未结 5 1205
生来不讨喜
生来不讨喜 2021-02-19 18:59

I try to do the following:

QList a;
foreach(QString& s, a)
{
    s += \"s\";
}

Which looks like it should be legitimate but

5条回答
  •  执念已碎
    2021-02-19 19:24

    I believe Qt's foreach takes a temporary copy of the original collection before iterating over it, therefore it wouldn't make any sense to have a non-const reference as modifying the temporary copy it would have no effect.

提交回复
热议问题