Results 1 to 8 of 8

Thread: Does QList use copy constructor when some element append to it?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Does QList use copy constructor when some element append to it?

    does QList copy the element?
    or does it keep only "the memory address -- reference"?
    (so do i need to free my "pointer" element?)

    eg.
    Qt Code:
    1. QList list;
    2. Element *e;
    3. while (query.next()) {
    4. e = new Element();
    5. ...
    6. list << *e;
    7. delete e; // required?
    8. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Does QList use copy constructor when some element append to it?

    It copies the element. In your case, the better code would be:
    Qt Code:
    1. QList list;
    2. while (query.next()) {
    3. Element e;
    4. ...
    5. list << e;
    6. }
    To copy to clipboard, switch view to plain text mode 

    It's documented.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Does QList use copy constructor when some element append to it?

    Have you looked into docs ? There is clearly stated:
    Internally, QList<T> is represented as an array of pointers to items of type T. If T is itself a pointer type or a basic type that is no larger than a pointer, or if T is one of Qt's shared classes, then QList<T> stores the items directly in the pointer array.
    What's the purpose of having
    Qt Code:
    1. QList<Element> list;
    To copy to clipboard, switch view to plain text mode 
    rather than
    Qt Code:
    1. QList<Element*> list;
    To copy to clipboard, switch view to plain text mode 
    ?
    This way you can delete objects after you're done using them. It's better design, IMHO, than this:
    Qt Code:
    1. e = new Element();
    2. ...
    3. list << *e;
    4. delete e;
    To copy to clipboard, switch view to plain text mode 
    Here you are creating element, store it in list, so its obvious that you're going to use it in the future, but in the next line you are deleting it, as if you don't need it anymore.
    Just store pointers and delete them when you are done.

  4. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Does QList use copy constructor when some element append to it?

    Quote Originally Posted by stampede View Post
    Qt Code:
    1. e = new Element();
    2. ...
    3. list << *e;
    4. delete e;
    To copy to clipboard, switch view to plain text mode 
    Here you are creating element, store it in list, so its obvious that you're going to use it in the future, but in the next line you are deleting it, as if you don't need it anymore.
    Just store pointers and delete them when you are done.
    Actually that code stores a copy of *e, not a copy of e. The local object pointed at is deleted, not the one in the list. Those are two different instances. The fact that QList might be using a pointer to an object internally is irrelevant here.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  5. #5
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Does QList use copy constructor when some element append to it?

    Here you are creating element, store it in list, so its obvious that you're going to use it in the future, but in the next line you are deleting it, as if you don't need it anymore.
    Just store pointers and delete them when you are done.
    it's just an example
    actually, i need to put the something in a collection class "that save only the pointer", not cloning it...
    like saving a Tcp Socket... (from what i learned here... QList is not suitable collection class for this, cmiiw)

    Qt Code:
    1. delete e; // required?
    To copy to clipboard, switch view to plain text mode 
    see the comment i put... i wondered if i need those line before
    i expect its only to keep pointer, not copying it...
    but responsible for freeing when the list's desctructor is called


    simply, i looking for replacement of TObjectList in Pascal
    it keep only pointer
    but responsible for freeing also

    thanks for all reply

  6. #6
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Does QList use copy constructor when some element append to it?

    If you store a pointer in a list, don't delete the object. You will have a dangling pointer.

    QList is not responsible for deleting objects. You can use QSharedPointer<> for that, provided that you don't setParent() on your QObjects:
    Qt Code:
    1. QList<QSharedPointer<QTcpSocket> > list;
    2. list << QSharedPointer<QTcpSocket>(new QTcpSocket);
    3. list.last()->doTheConnectStuff();
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  7. #7
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Does QList use copy constructor when some element append to it?

    thank you,

    QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.
    what does "out of scope" means here?

    outside of { ... } it's curly brace?
    Qt Code:
    1. {
    2. QTcpSocket *socket = new QTcpSocket();
    3. QSharedPointer<QTcpSocket> socketHolder(socket);
    4.  
    5. } // socket will auto deleted here?
    To copy to clipboard, switch view to plain text mode 

    i still confuse with your example (put inside parameter)
    sorry for my slow brain... need to break down
    Qt Code:
    1. {
    2. QTcpSocket *socket = new QTcpSocket();
    3. QSharedPointer<QTcpSocket> socketHolder(socket);
    4. list << socketHolder;
    5. } // **
    To copy to clipboard, switch view to plain text mode 

    ** socketHolder already out side of scope, does it delete socket?
    so does it means QSharedPointer that get copied inside list point to invalid address?

    or

    (since it named 'Shared')
    it will not delete the object if there other QSharedPointer still holding the same object.
    that's means only the last QSharedPointer will responsible for deleting?

  8. #8
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Does QList use copy constructor when some element append to it?

    This:
    it will not delete the object if there other QSharedPointer still holding the same object.
    that's means only the last QSharedPointer will responsible for deleting?
    QSharedPointer deletes the object when the last reference has gone out of scope. If you add one to a QList, there is still a reference in scope.

    Reading material: QSharedPointer, Count with me: How many smart pointer classes does Qt have?.
    Last edited by franz; 17th December 2010 at 19:49. Reason: updated contents
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  9. The following user says thank you to franz for this useful post:

    indomie_seleraku (18th December 2010)

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:54
  2. QList append() problem
    By reuabreliz in forum Qt Programming
    Replies: 6
    Last Post: 6th January 2010, 13:27
  3. Change a QList element
    By mcrahr in forum Newbie
    Replies: 1
    Last Post: 23rd August 2009, 11:16
  4. QVector copy constructor
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2008, 17:52
  5. QStandardItemModel: copy and append
    By davisjamesf in forum Newbie
    Replies: 1
    Last Post: 19th September 2007, 16:23

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.