Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt 4.5 - QList::QList(const QList&) - is this a deep copy constructor?

Tags:

qt

qt4

I am getting confused about the QList copy constructor by the documentation.

QList::QList ( const QList & other ) Constructs a copy of other.

This operation takes constant time, because QList is implicitly shared. This makes returning a QList from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.

Then on the link about being implicitly shared, it talks about reference counting and copy-on-write. Is this a deep copy or just a shallow copy?

like image 371
Extrakun Avatar asked Aug 14 '09 10:08

Extrakun


People also ask

What is QList in Qt?

QList<T> is one of Qt's generic container classes. It stores items in a list that provides fast index-based access and index-based insertions and removals.

What is QList?

The QList class is a template class that provides lists. QList<T> is one of Qt's generic container classes. It stores a list of values and provides fast index-based access as well as fast insertions and removals. QList<T>, QLinkedList<T>, and QVector<T> provide similar functionality.

How do I iterate through a QList?

QList<T>::iterator allows you to iterate over a QList<T> (or QQueue<T>) and to modify the list item associated with the iterator. If you want to iterate over a const QList, use QList::const_iterator instead.

How do you create an array in Qt?

In CPP file, inside that function: itemVal[valID] = new mListItem(); itemVal.at(valID)->setText(QString::number(val)); itemVal.at(valID)->setTextAlignment(Qt::AlignRight); ui->tableWidMemory->setItem(MWindow::tabRows, 1, itemVal.at(valID));


2 Answers

It's a shallow copy. A deep copy of the data happens behind the scenes the first time you call a non-const function on the copy or the original list.

like image 168
rohanpm Avatar answered Sep 18 '22 08:09

rohanpm


This operation takes constant time, because QList is implicitly shared.

If you don't modify the list, those are shared ! So Behind the scene, you read at the same address the informations !

If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.

But if you modify the copy list, there is no other choice that copy effectively the list ! And so, you have a linear cost depending on the list size.

from qt doc on copy on write and shared memory :

A deep copy implies duplicating an object. A shallow copy is a reference copy, i.e. just a pointer to a shared data block. Making a deep copy can be expensive in terms of memory and CPU. Making a shallow copy is very fast, because it only involves setting a pointer and incrementing the reference count.

So if you don't modify the list you read the information at the same address that the list given as parameter, it's called shallow copy. And if you modify it, you will have a deep copy of the list.

like image 23
Matthieu Avatar answered Sep 20 '22 08:09

Matthieu