Solved Implementation of QVector
-
According to the documentation over at https://doc.qt.io/qt-5/qvector.html#at there is only one implementation for QVector::at(), of which applies constness.
I just assumed from my STL expierience that there would be one which is non-const.
version of the function what would make sense in my opinion.
I got the error with the following codesnippet, please explain if i get something entirely wrong :)void MyClass::on_MyComboBox_currentTextChanged(const QString& newtext) { auto index = _ui->MyComboBox->currentIndex(); _myQVector.at(index) = arg1; for(const auto& element : _myQVector) qInfo() << element; }
-
You can't assign like this. at returns a value and not variable
-
Hi
Oddly enough, there is no non const version of at()
However, the operator [] has one
https://doc.qt.io/qt-5/qvector.html#operator-5b-5d -
@dheerendra said in Implementation of QVector:
[..] at returns a value and not variable
Yeah but why is that the case? As far as i know there could be a non-const version like this:
T& QVector::at()
For the
operator[]
there is a non-const and a const one, why is this not the case withat()
?! -
@ChristianHeusel the soley reason
at()
exists is to provide a read-only access to make sure the implicitely shared container does not detach.For write access, use
[]
regards
-
@aha_1980 Supposing that your answer is correct my question is now invalid since the purpose of
std::vector::at()
is then a diffrent one than it is in QT.
Question will be marked as resolved :) -
@ChristianHeusel it's indirectly stated here: https://doc.qt.io/qt-5/qvector.html#operator-5b-5d (a bit hidden)
"non-const operators can cause a deep copy".