title: Delete a list in c++
body: <p>I have classes A and B. Class B has a pointer to class A as a field, pointing to a list:</p>
  
  <pre><code> struct A {
        A* next;
        char* txt;
      ...};
  struct B {
        A* next;
        int length;
      ...};
  </code></pre>
  
  <p>I make each element of this list in this way:</p>
  
  <pre><code>void B::add_elem(char* str) {
    A* tmp;
    tmp = new A;
    tmp-&gt;txt = str;
    tmp-&gt;next = next;
    next = tmp;
  }
  </code></pre>
  
  <p>And I have to delete the list at the end. Will it be deleted by default with a destructor of class A, as I've called <code>new A</code>(but I made my destructors to print messages, there were no ones) or should I delete it explicitly (I've tried, it was a mistake in compilation)? Help, please.</p>
  
  <pre><code>B::~B() {
    int i;
    A* tmp;
    cout&lt;&lt;"del list\n";
    if (next) { // at least one elem                                                                                                    
      tmp = next;
      while (tmp-&gt;next) {
        tmp = tmp-&gt;next;
        delete next;
        cout&lt;&lt;"del elem\n";
        next = tmp;
      }
      delete tmp;
    }
  }
  </code></pre>