I have a templated class, myFoo, which stores "stuff" of type T which can be either primitive or pointers to complex types. When myFoo is deleted, I want to release all the memory associated with everything it happens to be storing. This means I need to call delete on every pointer being stored but I might also end up calling delete on a primitive. Is this safe??
I've included a sketch of myFoo below to better highlight what's going on. I'm not sure if the behaviour of the destructor is well defined.
template<class T>
class myFoo
{
public:
myFoo(int size)
{
size_ = size;
T* foo = new T[size_];
}
void addFoo(T tmp, int index)
{
foo[index] = tmp;
}
virtual ~myFoo()
{
for(int i=0; i < size_; i++)
{
delete foo[i];
}
delete [] foo;
}
private:
int size_;
T* foo;
}