Deep copy of QJsonObject (& QJsonArray)
-
QJsonObjecthas a copy constructor:QJsonObject::QJsonObject(const QJsonObject &other)The documentation says:
Creates a copy of other.
Since QJsonObject is implicitly shared, the copy is shallow as long as the object does not get modified.Does the "as long as" mean that if I subsequently make a change to the copy, Qt will automatically convert this to a deep copy?
e.g.
auto myCopy = QJsonObject(myOriginal); myCopy["foo"] = "bar"(And the same for
QJsonArray.) -
QJsonObjecthas a copy constructor:QJsonObject::QJsonObject(const QJsonObject &other)The documentation says:
Creates a copy of other.
Since QJsonObject is implicitly shared, the copy is shallow as long as the object does not get modified.Does the "as long as" mean that if I subsequently make a change to the copy, Qt will automatically convert this to a deep copy?
e.g.
auto myCopy = QJsonObject(myOriginal); myCopy["foo"] = "bar"(And the same for
QJsonArray.)@paulmasri
Yes I believe that is indeed what happens. I have not tested, but I imagine you will now find thatmyOriginal["foo"] != "bar", that would demonstrate? -
QJsonObjecthas a copy constructor:QJsonObject::QJsonObject(const QJsonObject &other)The documentation says:
Creates a copy of other.
Since QJsonObject is implicitly shared, the copy is shallow as long as the object does not get modified.Does the "as long as" mean that if I subsequently make a change to the copy, Qt will automatically convert this to a deep copy?
e.g.
auto myCopy = QJsonObject(myOriginal); myCopy["foo"] = "bar"(And the same for
QJsonArray.)Does the "as long as" mean that if I subsequently make a change to the copy, Qt will automatically convert this to a deep copy?
Yes, see implicit sharing.
Basically copied objects share state until an attempt to modify any of them is made, at which point a deep copy is made and modified. This mechanism is also sometimes called COW (copy on write).