Thread: destructors

  1. #1
    Registered User
    Join Date
    Oct 2002


    In my program I have to put a destructor in the class which will delete all the links when a linklist object is distroyed. I have to delete each link as it goes, displaying a message that the element was destoryed.....this is the code for the class....

    struct link                                             //one element of list
            int data;                                       //data item
            link* next;                                     //Pointer to next link
    class linklist
                    link* first;                            //pointer to first link
                    linklist() : first(NULL)
                    { }
                    void additem(int d);
                    void display() const;
    void linklist::additem(int d)
            link* newlink = new link;                       //make a new link
            newlink->data = d;                              //give it data
            newlink->next = first;                          //it points to next link
            first = newlink;                                //now first points to this
    void linklist::display() const
            link* current = first;                          //set ptr to first link
            while(current != NULL)                          //quit on last link
                    cout<<current->data <<endl;             //print data
                    current = current->next;                //move to next link
    Anyhelp on where this destructor goes would be great.


  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    You'll need something like this:
    if next
         delete next
    in the destructor, as well as the message displaying that the element was deleted. Figure out why this works - it's good for you.

    Oh, and the placement of the destructor in the file doesn't matter.

    void linklist::~linklist()

  3. #3
    Registered User
    Join Date
    Feb 2002
    ive always been taught to do this with my linked lists:

    you will probably want a function to remove a node from the end of your list. i would make its prototype like this:

    template <typename Type>
    bool remove( Type &t );

    where type is the type of variable the list holds. the way it works is, you pass a variable in to t to store the data of the node youre deleting, and then you return true if a node was deleted or false if the list is empty.

    then all you have to do in your destructor is call the remove function in a while loop until it returns false. i usually would make a dummy variable to hold the value, just so the function has the argument it needs.

       Type temp
       while ( remove(temp) );
    I came up with a cool phrase to put down here, but i forgot it...

  4. #4
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    how about something like:
    while(count) // while there are nodes
        RemoveNode; // call the function you normally call when deleting a node
                    //this should also decrement count
    This is assuming you're keeping a counter in your list class of how many nodes you have.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a class have multiple destructors?
    By meili100 in forum C++ Programming
    Replies: 1
    Last Post: 05-14-2008, 05:28 PM
  2. Destructors in STL?
    By sawer in forum C++ Programming
    Replies: 4
    Last Post: 08-09-2006, 09:35 AM
  3. I always have memory errors in destructors
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2004, 03:07 PM
  4. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 11:26 PM
  5. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM