[ACCEPTED]-Qt list.clear() does it destroy the object's?-qlist

Accepted answer
Score: 13

No, it just removes them from the list. You 11 have to delete it on your own. Here can 10 you find the implementation to make sure:

template <typename T>
Q_OUTOFLINE_TEMPLATE void QList<T>::clear()
{
    *this = QList<T>();
}

and 9 here is the documentation saying that it 8 only removes them from the list, but it 7 is not deleting:

void QList::clear()

Removes all items from the 6 list.

If you want to delete them, I suggest 5 to use the following algorithm:

void qDeleteAll(ForwardIterator begin, ForwardIterator end)

Deletes all 4 the items in the range [begin, end) using 3 the C++ delete operator. The item type must 2 be a pointer type (for example, QWidget 1 *).

Example:

QList<Employee *> list;
list.append(new Employee("Blackpool", "Stephen"));
list.append(new Employee("Twist", "Oliver"));

qDeleteAll(list.begin(), list.end());
list.clear();
Score: 3

if you want them destroyed automatically 3 then you need to either delete them manually 2 or store a smart pointer (std::unique_ptr<Type>, QPointer<Type> (if Type is a QObject), ...) in 1 the list

More Related questions