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. QList shared class behaviour question
Forum Updated to NodeBB v4.3 + New Features

QList shared class behaviour question

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 209 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    I do not have Qt/compiler at hand, so excuse if I am wrong or could just have tested. This is a quick "brainstorm" thought....

    As one of Qt's implicitly shared classes I believe something like either of the following is allowed and causes sharing/no copy at least initially:

    const QList a = { ... } ;
    const QList b = a;
    
    QList a = { ... } ;
    QList b = a;
    

    I want a list from the outside world which is read-only to me, I want to create a list from it which I may modify but may not. There will be many of these lists and operations so speed is important. What about:

    const QList a = { ... } ;
    QList b = a;
    

    Assuming (untested) the second line compiles, does it still start out with a shared-no-copy until I modify it or does copying a const to a non-const do a copy right from the assignment time?

    P.S.
    While I am here. There isn't a very quick/easy/dirty way to see if a QList is or isn't shared with another instance is there? Like showing the ref count or which list they really each are? (This would be for debugging/performance.) I guess I have to go == to find out, I believe QList== does a "are these the same shared list thing" rather than comparing each item?

    1 Reply Last reply
    1
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What are you afraid of here? That the 'copy from' list is const? Doesn't matter - the internal ref count is increased no matter if the container is const or not.

      No, you can't see the refcount in simple way.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JonBJ 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        What are you afraid of here? That the 'copy from' list is const? Doesn't matter - the internal ref count is increased no matter if the container is const or not.

        No, you can't see the refcount in simple way.

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

        @Christian-Ehrlicher said in QList shared class behaviour question:

        That the 'copy from' list is const? Doesn't matter - the internal ref count is increased no matter if the container is const or not.

        Yep, just that. That it might have done an actual copy "in advance" when assigning a const to a non-const. So it will indeed still start out shared, I am happy for my purpose where there will be many copies but few modifications.

        1 Reply Last reply
        0
        • JonBJ JonB has marked this topic as solved on

        • Login

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