Is the following assignment operation on a QByteArray reference safe?
-
I have a function that takes a QByteArray object as input by reference and assigns another object inside, like the following
Result myfunc(QByteArray& bytes) { .... bytes = QByteArray(rawData, rawDataSize); .... } QByteArray byteArray; myfunc(byteArray); parse(byteArray);
Is this assignment operation within
myfunc
safe? I would expect it to be a safe operation for a std::vector but I am not sure about QByteArray (shallow-copy and deep-copy issues).PS - I would have preferred a return by value but can't change the API
-
I don't see a problem here. What should be the problem? If copy on write would change such fundamental things for the user/programmer than it would not be used by Qt. You don't need to think about cow in 99,99% of the time
-
-
@Christian-Ehrlicher I incorrectly assumed that QByteArray would always create a deep copy of the data which was not true for
QByteArray::fromRawData
(documented behaviour, that I didn't read). So just wanted to confirm if this would behave as intended.