View Full Version : How to copy a linklist

18th August 2010, 10:22

I am used to creating and using linklists in c but not used to the QLinkedList class, i have one setup and populated in the mainwindow function but then need to call a function and pass a copy of this linklist into it. The problem is it seems not to keep the two lists as seperate items and if i change one the other changes.

Is there a specific way to do this?



18th August 2010, 10:34
QLinkedList has a copy constructor (http://doc.qt.nokia.com/4.6/qlinkedlist.html#QLinkedList-2) witch initialize a new list with the shared elements of your original list and copy the list only if you modify the "copy" (copy-on-write)

18th August 2010, 10:43
Thanks for your quick responce my current bit of code is

ceguSubWidget::ceguSubWidget(QLinkedList<tabptr> tabs)
tablist = tabs;
isUntitled = true;
Is that correct or incorrect.


18th August 2010, 10:52
If you use the list to store pointers to type the copy of the list doesn't copy the objects pointed by the pointers stored in the list (it copy the pointers)

If tabptr is a type defined by you, code a copy constructor witch must do a deep copy (copy of objects the pointers points to)

LE: i think this is your problem: you end up with two pointers (two copy, one in each copy of the list) per each object, if you modify the object by de-reference one pointer, the second pointer will point to the same modified value (not the one before modification)

18th August 2010, 11:42
I created a copy constuctor and it working now.

Thanks for your help