Hey I wonder about I have written a C++ linked list, where I call a destructor to go through an allocated linked list and delete every node found. However what I found is that although it goes through the linked list and delete every occurance it will still print out values. Although just some scrap values.
But shouldn't it be when I delete the linked_list it shouldn't be printable next time?
I'm create a linked list by using the new, and delete when I remove the list
sorted_list::~sorted_list()
{
// Destructor implementation
destroy(this->first);
cout << "Destructor called sorted_list" << endl;
}
void sorted_list::destroy(list_link* item)
{
if (item)
{
destroy(item->next);
delete item;
}
}
print function
void sorted_list::print() {
if(this->first)
{
iteratorn *traverse = new iteratorn(this->first);
while( !traverse->iterator_end() )
{
cout << traverse->iterator_get_key() << " ";
traverse->iterator_next();
}
delete traverse;
}
else
cout << "list empty" << endl;
}
NULL(or even 0) tothis->firstin the destructor. The list is released, it's gone, utterly inaccessible.