Can I call delete on primitives?

故事扮演 提交于 2019-12-07 06:22:50

问题


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;
}

回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!