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. Qt Containers, Implicit Sharing and Passing Parameters
Forum Updated to NodeBB v4.3 + New Features

Qt Containers, Implicit Sharing and Passing Parameters

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 1.6k 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.
  • I Offline
    I Offline
    isaacEnrique
    wrote on last edited by
    #1

    Greetings.

    I've been reading about Qt Containers and Implicit Sharing and emerged me a question about passing parameters of Qt Containers. In particular, I wonder:

    Is it worth using references when I pass Qt Containers by parameters or the Implicit Sharing mechanism makes this unnecessary?

    For efficiency (prevent copying) I always use references (constant or not, as appropriate) to objects passed by parameters. Then by example, with Qt Containers... Which is better:

    Use 'const QList< MyObject >&' or simply 'QList< MyObject >'?

    Thanks in advance for any reply or comment.

    Isaac Pérez
    Programming is understanding.

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      Const references are still recommended.

      Implicit sharing makes it cheap to create copies, but it's still not free.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • I Offline
        I Offline
        isaacEnrique
        wrote on last edited by
        #3

        Thanks for your answer JKSH.

        And in the case of functions that return Qt Containers... Is it better to use references?

        Isaac Pérez
        Programming is understanding.

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          You're welcome :)

          It is usually dangerous to return references, because the original might be modified (or even destroyed). Your should return copies.

          Example: The reference returned by badFunc() will become invalid when the function returns, because the original string gets destroyed:
          @
          // Wrong:
          const QString& badFunc() {
          QString str = "Hello World!"
          return str;
          }

          // Right:
          QString goodFunc() {
          QString str = "Hello World!"
          return str;
          }
          @

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • I Offline
            I Offline
            isaacEnrique
            wrote on last edited by
            #5

            Thanks again JKSH.

            bq. It is usually dangerous to return references, because the original might be modified (or even destroyed). Your should return copies.

            The issue is that my program return a reference to a member (one QList of pointers to a QGraphicsItem subclass) of a own custom class.

            Given what you've described so far, I think in this case, return a reference would be more efficient... since due to the mechanism of Implicit Sharing, return a copy of the member would end up being the same.

            Am I right?

            Isaac Pérez
            Programming is understanding.

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              [quote author="IsaacEnrique" date="1422460420"]Given what you've described so far, I think in this case, return a reference would be more efficient... since due to the mechanism of Implicit Sharing, return a copy of the member would end up being the same.

              Am I right?[/quote]Possibly. I don't know if you'll be able to detect any improvement. (You can try to measure it -- that's the only way to tell if something is more efficient)

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • I Offline
                I Offline
                isaacEnrique
                wrote on last edited by
                #7

                Well, I guess I'll have to try to measure it.

                Thanks for everything.

                Isaac Pérez
                Programming is understanding.

                1 Reply Last reply
                0

                • Login

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