Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Deep copy of QJsonObject (& QJsonArray)
Qt 6.11 is out! See what's new in the release blog

Deep copy of QJsonObject (& QJsonArray)

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 898 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    paulmasri
    wrote on last edited by
    #1

    QJsonObject has 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.)

    JonBJ Chris KawaC 2 Replies Last reply
    0
    • P paulmasri

      QJsonObject has 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.)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @paulmasri
      Yes I believe that is indeed what happens. I have not tested, but I imagine you will now find that myOriginal["foo"] != "bar", that would demonstrate?

      1 Reply Last reply
      2
      • P paulmasri

        QJsonObject has 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.)

        Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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).

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved