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;
}
The only thing you can call delete
on is a pointer type. It's an error to call delete
on an int
, 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.
Template specialization
template <class T> struct delete_it;
template <class T> struct delete_it<T*>
{
static void func(T* ptr) { delete ptr; }
};
template <> struct delete_it<int>
{
static void func(int) {}
};
template <> struct delete_it<double>
{
static void func(double) {}
};
Repeat for all primitive types. Then
virtual ~myFoo()
{
for(int i=0; i < size_; i++)
{
delete_it<T>::func(foo[i]);
}
delete [] foo;
}
Unchecked code.
来源:https://stackoverflow.com/questions/11643395/can-i-call-delete-on-primitives