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. Returning QList with local scope variables
QtWS25 Last Chance

Returning QList with local scope variables

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.9k Views
  • 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.
  • B Offline
    B Offline
    bright day
    wrote on last edited by
    #1

    Hi!
    In doSmth() I create 2 objects of class Foo, and they will be destroyed as soon as doSmth() have excecuted. If I add f1 and f2 to QList, why they still can be accessed through QList<Foo> b = doSmth(); how it comes, if f1 and f2 are not copied when appended to QList, that I still can access them through result of doSmth ? (AFAIK, QList<T> stores pointers to type T)

    @
    class Foo
    {
    ...
    };

    QList<Foo> doSmth()
    {
    Foo f1,f2;
    QList<Foo> list;
    list.append(f1);
    list.append(f2);
    return list;
    }@

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      The way you coded it, QList stores objects of type Foo, which means it creates objects on the heap and c0opies the contents. QList ALWAYS stores a copy of the data you put in (as all other containers also do).

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

        As I understand it, (generally speaking - there are subtleties when storing pointer-sized data, etc. - the QList docs go into more detail) while QList internally uses a list of pointers to store (and implicitly share) data, it uses a copy of any data stored on the list.

        So while your f1 and f2 are only scoped locally, a copy of them gets appended to the list, and those copies are implicitly shared with the returned QList.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bright day
          wrote on last edited by
          #4

          That helped me, thank you both

          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