Is there a Pointer Based QMap or Similar
Hi,
I have two classes a Model and a Dataset
Suppose I need to create a map between two Pointers. for Egs
Code:
QMap<Model*, DataSet* > mapModelPtrToDataSet;
The code is working and I get the DataSet* for the Model* That i give to the Map
But Since I understood recently that the QMap is ValueBased... And all the DataSet* would not get delete in the Destructor of the Map. Is there an Pointer Based alternative ?
Thanks in Advance
Re: Is there a Pointer Based QMap or Similar
No, but you should be able to use qDeleteAll(mapModelPtrToDataSet.values()) to do that.
Re: Is there a Pointer Based QMap or Similar
But what if the DataSet* in the Map Gets Deleted Externally ?
will the qDeleteAll cause seg faults ??
Re: Is there a Pointer Based QMap or Similar
Yes, of course. But it's the programmers responsibility not to delete objects he/she doesn't own. If you want a QMap with pointers to objects you don't own and those objects inherit QObject, you can use:
Code:
QMap<Model *, QPointer<DataSet> > map;
In Qt3 you can use QGuardedPtr instead of QPointer. With such a mechanism qDeleteAll() should not make any harm as QPointer sets its internal pointer to 0 when the object it references gets deleted. And deleting null pointers is safe. On the other hand deleting pointers which you don't own doesn't make much sense, so the situation is purely academic.