Results 1 to 5 of 5

Thread: QList question

  1. #1
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QList question

    Hi.. I think this is silly question... but still

    Can we use pointer to QList ?? say QList<int> * pList; ??

    what are the benefits and pitfalls for using the pointer to QList ??
    As far as I know using the pointer will help to avoid return by copy..... but how good or bad that design will be ??
    please comment ...

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QList question

    Quote Originally Posted by aamer4yu View Post
    Hi.. I think this is silly question... but still

    Can we use pointer to QList ?? say QList<int> * pList; ??

    what are the benefits and pitfalls for using the pointer to QList ??
    As far as I know using the pointer will help to avoid return by copy..... but how good or bad that design will be ??
    please comment ...
    Well , since most classes in Qt are implicitly shared including QList all assignments and creation of temporary list objects involves just a pointer assignment and a variable increment, it doesn't make much difference unless you manipulate with the copied object (for eg just traversing it) . You can also use const references in relevant places to avoid these too.
    But if you need serious optimizations you can use pointer to list.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList question

    what do u mean by -
    since most classes in Qt are implicitly shared including QList
    ??

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QList question

    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList question

    Quote Originally Posted by aamer4yu View Post
    Can we use pointer to QList ?? say QList<int> * pList; ??
    Sure! Why couldn't we? QList is a regular C++ class... See, all classes even those commonly created/used as objects/references can be instanciated/used as pointers but the contrary isn't true (widgets for instances must be passed as pointers because a copy, even an implicit one would cause a segfault...)

    Quote Originally Posted by aamer4yu View Post
    what are the benefits and pitfalls for using the pointer to QList ??
    As far as I know using the pointer will help to avoid return by copy..... but how good or bad that design will be ??
    please comment ...
    AFAIK the only advantage is when you actually want to share data between several objects. i.e. any modification done by one of the owner will be known to the others. On the contrary if you want modifications to cause forks implicitely Qt takes care of it and pointers make it tricky. As it has already been said, "return by copy" is painless and everythin with such classes is painless as long as you only use const references which can't cause deep copy.
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. QVariant, QList, QSettings
    By TheKedge in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2007, 16:50
  2. using Qlist with a class
    By Havard in forum Qt Programming
    Replies: 10
    Last Post: 24th February 2007, 20:38
  3. QList crash in destructor
    By mclark in forum Newbie
    Replies: 7
    Last Post: 6th December 2006, 16:27
  4. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 21:43
  5. Values not being appended in a QList
    By Kapil in forum Newbie
    Replies: 5
    Last Post: 21st April 2006, 11:20

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.