QList copy constructor
-
If I have the following code:
@QList<X*> l1;
QList<X*> l2 = new QList<X*>(l1);@Where X is some kind of class.
Will the copy constructor of QList automatically copy all its elements (by calling the elements copy constructor), or will just the pointers e copied(shallow copy).This means, will QList copy contructor automatically call the copy constructor of X (if present)?
-
QList calls the copy constructor of the template type. That type is "X*" = "pointer-to-X" (not "X"!), so the copy constructor of "pointer-to-X" is called (not a copy constructor of "X"), which just copies the value of the pointer (aka address of the pointed-to object). It neither does copy the referenced object nor calls any copy constructor of type X.
So, to make it short: the list elements are not copied. The elements in both lists point to the very same objects.
-
Ok, thank you.