I have a template class like this (with dynamic data):
template <class T>
class TemplateClass : public BaseClass {
public:
...
protected:
vector<T> data;
...
}
It's derived from a BaseClass because in another container class I must have all this template classes within the same vector:
class Container{
public:
...
protected:
vector<BaseClass*> elements;
}
This way I can have different types in the same vector and call TemplateClass methods just doing dynamic_cast (ej dynamic_cast<TemplateClass<int>*>(elements[i])->method(a)
("a" would be an integer)).
It works(the program runs) but it doesn't convince me because when I destroy the container I have to call explicitly the destructor of TemplateClass since BaseClass does not destroy data (vector):
delete dynamic_cast<TemplateClass<int>*>(elements[i])
I would like to use unique_pointer but I think it would not work because it doesn't delete data (since BaseClass destructor will be called instead of TemplateClass destructor and BaseClass cannot have this template vector).
Is there any alternative? I must have this TemplateClass vector.
Thank You