PDA

View Full Version : Is there a Pointer Based QMap or Similar



vasudhamirji
4th April 2006, 14:38
Hi,

I have two classes a Model and a Dataset
Suppose I need to create a map between two Pointers. for Egs


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

wysota
4th April 2006, 15:01
No, but you should be able to use qDeleteAll(mapModelPtrToDataSet.values()) to do that.

sunil.thaha
4th April 2006, 15:07
But what if the DataSet* in the Map Gets Deleted Externally ?
will the qDeleteAll cause seg faults ??

wysota
4th April 2006, 15:34
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:


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.