Thread: dynamic objects in a <list>

  1. #1
    Registered User
    Join Date
    Nov 2001

    dynamic objects in a <list>

    Someone clear this up for me...

    list<Order> lst_Orders;

    typedef list<Order *>::iterator it;

    Order *ptrOrder1 = new Order;
    Order *ptrOrder2 = new Order;
    Order *ptrOrder3 = new Order;


    for ( it = lst_Orders.begin(); it != lst_Orders.end(); it++ )
    delete *it; // not sure
    lst_Orders.remove(it); // not sure

    Is this correct ?

  2. #2
    Registered User
    Join Date
    Dec 2001
    try to use code tags in future mate

    typedef std::list<Order*> lst_orders; // this is a typedef, not an instance
    typedef lst_orders::iterator lst_it; // ditto
    lst_orders theList; // instantiate the list
    lst_it theIterator; // instantiate the iterator
    Order *ptrOrder1 = new Order;
    Order *ptrOrder2 = new Order;
    Order *ptrOrder3 = new Order;
    theList.push_back(ptrOrder1); // add to the list
    for ( theIterator = theList.begin(); theIterator != theList.end(); ++theIterator ) // make the sure the iterator is incremented first, coz theList.begin() isn't an item in the list
       delete *theIterator;
    hope this helps
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic objects
    By wierdbeard65 in forum C++ Programming
    Replies: 14
    Last Post: 06-26-2008, 04:13 AM
  2. Problem with pointers to dynamic objects
    By mike_g in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 01:16 PM
  3. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  4. dynamic array of objects
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2003, 09:35 AM
  5. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 02:05 PM