title: Destructor in template class c : How to delete field which may be pointer or not pointer?
body: <p>I have template class <code>Array</code> where the template type <code>T</code> should be either pointer or not-pointer type.</p>
  
  <pre><code>template&lt;class T&gt;
  class TArray
  {   
       static const int max_len=100;
       T* data;
       int length;
  
    public:
       TArray(){data=new T[max_len]; this-&gt;length=0;} 
       void Add(T value){data[length++]=value;}   
       ~TArray();
  };
  </code></pre>
  
  <p>The problem is how to free space, since we can not call <code>delete</code> for not pointer types like this</p>
  
  <pre><code>template&lt;class T&gt;
  TArray&lt;T&gt;::~TArray()
  {
      //for(int i =0; i &lt; length; i++)
      //  delete data[i]; // NOT WORKING WITH OBJECTS THAT ARE NOT POINTERS !!!
      delete[] data;
  }
  </code></pre>
  
  <p>Let's have addition class</p>
  
  <pre><code>class A
  {
      int* a;
    public:
      A(int n){a = new int[n];}
      ~A(){delete[] a;}
  };
  </code></pre>
  
  <p>and make two instances of template class</p>
  
  <pre><code>// Create array of doubles
  TArray&lt;double&gt;* double_array = new TArray&lt;double&gt;();
  delete double_array;
  
  // Create array of pointers to class A
  TArray&lt;A*&gt;* A_array = new TArray&lt;A*&gt;();
  A* a = new A(5);
  A_array-&gt;Add(a);
  delete A_array;
  </code></pre>
  
  <p>When I call destructor for <code>TArray&lt;A*&gt;</code> I need to call destructor for class <code>A</code> but I don't know how, since the commented code in destructor is not compile (C2541) if we make for example array of doubles.</p>