How do you collect garbage from a vector of a class pointer?

This is a discussion on How do you collect garbage from a vector of a class pointer? within the C++ Programming forums, part of the General Programming Boards category; Hello. How do you collect garbage from a vector of a class pointer (let's say, MyClass*)? Since I'm just barely ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    How do you collect garbage from a vector of a class pointer?

    Hello. How do you collect garbage from a vector of a class pointer (let's say, MyClass*)? Since I'm just barely starting out with STL, a really simple and easy to follow solution would be most appreciated. I'm guessing you can just go through it with an iterator and keep calling delete on the dereferenced iterator, but that doesn't sound like a very good way to do it. Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,307
    Yes, that is a way to do it. If you don't actually need to store pointers in the vector, a simpler solution is to use a vector<MyClass>. If you must store pointers, a more robust solution is to use vector<shared_ptr<MyClass>> (or vector<unique_ptr<MyClass>>, depending on your needs), but std::shared_ptr and std::unique_ptr are new additions to the C++ standard library and so might not be available.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    Thanks much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collect multiple lines of inputs
    By drag0n69 in forum C Programming
    Replies: 10
    Last Post: 02-25-2008, 12:45 AM
  2. Replies: 2
    Last Post: 09-09-2007, 01:58 AM
  3. Making stack class using vector class
    By spank in forum C++ Programming
    Replies: 9
    Last Post: 08-10-2007, 04:50 AM
  4. finding derived class type of a pointer to a base class
    By LinuxCoder in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2006, 12:08 PM
  5. Collect Coins & Score System
    By Mecnels in forum Game Programming
    Replies: 4
    Last Post: 12-07-2004, 01:42 PM

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