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. Implicit Sharing
Forum Updated to NodeBB v4.3 + New Features

Implicit Sharing

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 510 Views 2 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.
  • prinzkmP Offline
    prinzkmP Offline
    prinzkm
    wrote on last edited by prinzkm
    #1

    Hi friends, I have read that implicit sharing is a clever methodology in saving resources. In that case, Can any one please explain how I can verify two QString objects in which one is allocated with some data and the other one is assigned with the former one.

    Say
    QString a="aaa"
    QString b=a;//shallow copy
    qDebug()<<a.toStdString().c_str();//0x100
    qDebug()<<b.toStdString().c_str();//0x101
    Why do I get two different pointer addresses if shallow copy was being done?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Because toStdString returns a std::string and c_str() returns the address of the first char from that std::string object.

      Note that calling it the way you do, will also get you different values for different calls because of the lifetime of the objects involved.

      In any case, the pointer you are getting has just no relation with the original QString objects.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • prinzkmP Offline
        prinzkmP Offline
        prinzkm
        wrote on last edited by
        #3

        Is there no way of knowing that both are pointing to same content? So it is a bad practice to use toStdString() over and over for the same variable.Isn't it?

        jsulmJ 1 Reply Last reply
        0
        • prinzkmP prinzkm

          Is there no way of knowing that both are pointing to same content? So it is a bad practice to use toStdString() over and over for the same variable.Isn't it?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @prinzkm said in Implicit Sharing:

          So it is a bad practice to use toStdString() over and over for the same variable.Isn't it?

          Yes. You can assign what toStdString() returns to a variable instead of calling it over and over.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • prinzkmP Offline
            prinzkmP Offline
            prinzkm
            wrote on last edited by
            #5

            I found the answer :) . isSharedWith() function returns true if two implicitly shared objects are pointing to the same.
            ie a.isSharedWith(b) returns true in my case.This will remain true until either of it is modified.

            1 Reply Last reply
            1

            • Login

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