Results 1 to 10 of 10

Thread: QList with pointers

  1. #1
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QList with pointers

    Is there a way to use QList with pointers like in java ?

    I mean if I have somthing like that:
    QList<Number *> list;
    list.append(new Number(2));
    list.append(new Number(4));

    and get the correct answer on :
    list.contains(new Number(4));

    thanx
    Eli

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QList with pointers

    Quote Originally Posted by Rockem View Post
    I mean if I have somthing like that:
    QList<Number *> list;
    list.append(new Number(2));
    list.append(new Number(4));
    No problem, works fine.

    and get the correct answer on :
    list.contains(new Number(4));
    No, how could it work? "new" requesting for new space, so the location on your physical device (=pointer) could not be the same. If you want to check if there is a instance of Number with the value 4 you have to loop over the items. (or use any optimizations instead of a simple loop..)

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList with pointers

    The question is why would you want to use pointers in such a situation. To me Number should obviously always be allocated on stack.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList with pointers

    umm, I want to allocate number on the heap as it represent large object and I can have many of them on the list (also I move the list out of the scope)

    so contains doesn't support pointers ? even if the object implements == ?

    thanx
    Last edited by Rockem; 12th August 2009 at 16:52.

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList with pointers

    * QList will allocate memory on the heap internally... are you sure you want to do that yourself?
    * (the list going out of scope is another matter)
    * how big (in bytes) do you expect those number to be? how many do you want to store?
    * if you put a pointer inside QList, you will be responsible to delete the entries (QList will not delete them in its constructor); also comparison is done on the pointer, not the object pointed to. (maybe Boost.ptr_container can help you, if you insist on the pointer in list thing. An alternative might be to wrap the allocated object in "smart pointers" like boost::shared_ptr

    HTH

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList with pointers

    Quote Originally Posted by Rockem View Post
    umm, I want to allocate number on the heap as it represent large object and I can have many of them on the list (also I move the list out of the scope)
    It doesn't matter, it will work fine without pointers. Just make sure you implement the copy constructor and assign operator (unless the ones provided by the compiler are fine) and think about making your Number an implicitly shared class (see QSharedData).

    so contains doesn't support pointers ? even if the object implements == ?
    No, because the list contains pointers so it also compares pointers.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList with pointers

    what do you mean by "QList will allocate memory on the heap internally." ?

    I mean if I do :
    Number n(2);
    list.append(n);
    return list;

    n will be deleted right ?
    otherwise who will delete it

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList with pointers

    n will be deleted but its copy that is in the list will get copied again and returned as part of the list. That's why you need to implement the copy constructor for your class. And if you make Number an implicitly shared class, only the facade part of the object will be allocated on the stack and the real data will reside on heap and will not be copied whenever the object itself is copied.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2008
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList with pointers

    how do I make a class to be implicitly shared class ?

    thanx

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList with pointers

    Open Qt Assistant and type in "Implicitly Shared Classes" in the index tab. Then type in "QSharedDataPointer".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 18:55
  2. QList, copy problems
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 5th February 2010, 00:06
  3. QList Pointers
    By coderbob in forum Newbie
    Replies: 2
    Last Post: 20th November 2007, 18:50
  4. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43
  5. QList of pointers
    By Farcaller in forum Newbie
    Replies: 4
    Last Post: 24th January 2006, 16:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.