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;
}
Template specialization
Repeat for all primitive types. Then
Unchecked code.
The only thing you can call
delete
on is a pointer type. It's an error to calldelete
on anint
, for example. If you arrange your templates so that your code tries to do something that is an error, the compiler will let you know and refuse to compile your code.So no, you don't have to worry about "accidentally" deleting a non-pointer.