Custom object type and QList
Guys give some hint about operator overloading
I have such QList: QList<MyCustomType*> list;
What kind of operator==() , must have my custom type, to have ability use it in method QList::indexOf(MyCustomType*)
?
7 replies
Yes. However, you cannot do that with QObject derived types, as the types you put in your container must be copyable and default constructable. QObject does not meet those requirements. You can put pointer to a QObject in your container, but no delete will be called automatically on those pointers on destruction of the list object. That means that if that was the only pointer to object, you are leaking memory.
A last alternative is to store shared pointers in your list. QSharedPointer fulfils the requirements of types to put in a container, and when the list is destructed, they will automatically delete anything they pointed to if there are no other shared pointers pointing to the same data.
You must log in to post a reply. Not a member yet? Register here!



