Apr 15, 2013 at 2:36pm UTC
Sup?
let's say I have class LinkedList:
1 2 3 4 5 6 7 8 9 10 11 12 13
class LinkedList
{
public :
class Node
{
public :
Data* data;
Node* next;
};
Node* head;
public :
// methods.....
};
I do the following:
LinkedList* newL = new LinkedList() // constructor: LinkedList() { head = NULL; }
Now I do things like add thing to newL:
1 2 3
newL->Add(x)
newL->Add(y)
newL->Add(z)
Now I want to delete newL. I can I do it?
This is the code of the D'tor:
1 2 3 4 5 6 7 8 9 10 11
LinkedList::~LinkedList()
{
Node* current = head;
while ( current != NULL )
{
Node* next = current->next;
delete current;
current = next;
}
head = NULL;
}
Do delete newL:
1) (*newL).~LinkedList
2) delete newL
3) delete[] newL
4) none of the above ;)
If the answer is 4, what should I do?
any help will be appreciated.
Last edited on Apr 15, 2013 at 2:38pm UTC
Apr 16, 2013 at 2:43pm UTC
Yes, (2) calls the destructor of newL and then releases the memory used by that LinkedList object. The destructor removes each Node object that was on the list. What remains uncertain is what do the Add and ~Node members of each Node do to the Data *.