Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ List destructor called after every use of List's method
    text
    copied!<p>This is a follow up to a question I asked earlier today regarding the appropriation of directly calling a class' destructor.</p> <p>I'm creating my own List for an assignment. I have overloaded the assignment, square brackets, in and out stream operators and have the basic adding, removing and printing to screen functionality.</p> <p>My next step is to have the List delete and release the memory of all of its nodes, which I am having trouble with.</p> <p>I have the List write to the screen its current state after performing its operations, as follows:</p> <pre><code>void main() { // Create instance of list List aList(true); List bList(true); aList.Add(1); aList.Add(2); bList.Add(2); cout &lt;&lt; "aList: " &lt;&lt; aList; cout &lt;&lt; "bList: " &lt;&lt; bList; aList = bList; cout &lt;&lt; "aList: " &lt;&lt; aList; cout &lt;&lt; "bList: " &lt;&lt; bList; aList.Add(3); cout &lt;&lt; "aList: " &lt;&lt; aList; cout &lt;&lt; "bList: " &lt;&lt; bList; int i = 0; cin &gt;&gt; i; } </code></pre> <p>Here is my overloaded out stream:</p> <pre><code>ostream &amp;operator&lt;&lt;(ostream &amp;stream, List theList) { stream &lt;&lt; "There are " &lt;&lt; theList._totalNodes &lt;&lt; " nodes in the list." &lt;&lt; endl; for(int i = 0; i &lt; theList._totalNodes; i++) { stream &lt;&lt; "Node #" &lt;&lt; i &lt;&lt; " holds the data: " &lt;&lt; theList[i] &lt;&lt; endl; } return stream; } </code></pre> <p>Looking at the command prompt window after this is used "Destructor called" is also printed underneath it.</p> <p>The List destructor:</p> <pre><code>List::~List() { cout &lt;&lt; "Destructor called" &lt;&lt; endl; // List::Destroy(); } </code></pre> <p>The overloaded assignment operator also seems to call the destructor, which doesn't surprise me. However, the others do.</p> <pre><code>List &amp;List::operator=(List &amp;aList) { // Make sure not the same object trying to be copied if(this != &amp;aList) { // Perform deep copy // Delete old pointers in redundant list, only if they exist already if(_head) { delete _head, _tail, _current, _del; _totalNodes = 0; // Reset total nodes } // Now set new pointers copied from other list _head = NULL; _tail = _head; _current = _head; _del = _head; // Now loop through all nodes in other list and copy them to this // Reset pointer in other list aList.Reset(); for(int i = 0; i &lt; aList._totalNodes; i++) { Add(aList.Current()-&gt;_data); aList.Next(); } // Reset other list's pointer aList.Reset(); Reset(); // Reset our pointer } return *this; // Return for multi assignment } </code></pre> <p>I also have the destructor to a List write "Destructor called" when it's called and noticed that it is called after every use of one of its methods.</p> <p>Why is this? I assumed the destructor is called when the object is no longer needed, i.e. deleted.</p> <p>Furthermore, when stepping through my code I have noticed that when a pointer is deleted the memory address it is pointing to isn't being nullified. Do I have to manually NULL a pointer after it is deleted?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload