In a library I am developing I have a class that I want to have a hash storing any kind of data. This is because an algorithm using this library may want to store specific data in the objects of that class.
The class is called “ObjectOfInterest” where I have defined the hash using QT:
QHash<QString, void*> properties;
Then I have implemented this functions to store information:
bool ObjectOfInterest::hasProperty(QString prop) const
{
return properties.contains(prop);
}
template <class T> const T& ObjectOfInterest::getProperty(const QString prop) const
{
return *(T *)properties.value(prop);
}
template <class T> void ObjectOfInterest::setProperty(const QString prop, T value)
{
if (hasProperty(prop)) deletePropertyValue<T>(prop);
properties.insert(prop, new T(value));
}
//private
template <class T> void ObjectOfInterest::deletePropertyValue(const QString prop)
{
delete (T *)properties.value(prop);
}
Now the question is, when deleting an object of “ObjectOfInterest”, how can I delete all values stored in the properties hash? For now I have
ObjectOfInterest::~ObjectOfInterest()
{
//delete other stuff...
QHash<QString, void*>::iterator i;
for (i = properties.begin(); i != properties.end(); ++i)
{
delete i.value();
}
}
But this is not a solution, as I am not calling the destructor. Any ideas how to do this?
Thanks!
Finally the solution was using boost::any