is there a difference between having a destructor in or out of the class? which one is better? having the destructor inside the class seems more readable. but my book seems to have the destructor accessed within the scope of the class.
Code://////////////////////////////////////////// class linklist //a list of links { private: link* first; //pointer to the first link public: linklist() //no-arg constructor { first = NULL; //no first link } ~linklist() //destructor { link* current = first; //set ptr to first link while( current != NULL ) //quit on last link { link* temp = current; current = current->next; delete temp; cout << "object destroyed." << endl; } } void additem(int d); //add data item (one link) void display(); //display all links }; //------------------------------------------ void linklist::additem(int d) //add item { link* newlink = new link; //make a new link newlink->data = d; //give it data newlink->next = first; //it points to the next link first = newlink; //now first points to this } //------------------------------------------ //linklist::~linklist() //{ // link* current = first; //set ptr to first link // while( current != NULL ) //quit on last link // { // link* temp = current; // current = current->next; // delete temp; // cout << "object destroyed" << endl; // } //}