When should I delete Objects ?

This is a discussion on When should I delete Objects ? within the C++ Programming forums, part of the General Programming Boards category; Hello all, I have the following code which prints out all the videos I have stored in a linked list ...

  1. #1
    Unregistered
    Guest

    Question When should I delete Objects ?

    Hello all,

    I have the following code which prints out all the videos I have stored in a linked list :

    void viewList(list<vid> &li)
    {
    system("cls");

    cout<< "\t\t\t\t *List of films*\n";

    cout<< "\t\t\t*****************************\n"<<endl;
    for(it=li.begin(); it!=li.end(); ++it)
    {
    vid t;
    t = *it;
    cout << "\t\t\t" << t.getTitle() << endl;
    }
    cout<< "\t\t\t*****************************\n"<<endl;
    system("PAUSE");
    system("cls");
    }

    What I want to know is if this code is BAD ? Do I have to delete the vid Object t which I use to enable me to print out the title each time.

    If so do I do this in a destructor and how ??

    Thank you

    Blim

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You usually delete objects as soon as they're no longer needed if you're using a large program, but if it is a matter of indifference, just at the end. If deleting objects becamse a major task for you, you may want to try using Java. It's very similar, but has automatic object deletion. As soon as an object is no longer referenced, the JVM deletes it.

  3. #3
    Unregistered
    Guest
    I also wish to know how this is done though ?

    How does he delete the vid Object, and does he actually need to delete it ?

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    non-static non-global objects are deleted upon the end of their scope, but anything else requires manual deletion.
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot delete objects
    By theJ89 in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2006, 09:25 PM
  2. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  3. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  4. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM
  5. how to delete objects
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2001, 02:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21