Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 10th, 2005, 1:04 PM   #1
Konnor
Newbie
 
Join Date: Aug 2005
Posts: 23
Rep Power: 0 Konnor is on a distinguished road
deleting a whole vector?

can this be done in a few lines of code?

I have this at a certain point in time

vector<Bullet*> bulletList;

and want to delete the whole thing at another point in time..

ta,
Konnor is offline   Reply With Quote
Old Dec 10th, 2005, 1:13 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
You can't remove the vector like this, you can deallocate its content though, using:
bulletList.clear();
Polyphemus_ is offline   Reply With Quote
Old Dec 11th, 2005, 3:15 AM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 884
Rep Power: 4 The Dark is on a distinguished road
Assuming you want to call delete for each pointer in the vector:
for (vector<Bullet*>::iterator it = bulletList.begin(); it != bulletList.end(); it++)
  delete *it;
bulletList.clear();
The Dark is offline   Reply With Quote
Old Dec 11th, 2005, 4:54 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
A better approach (assuming Bullet is a concrete class and not a polymorphic base class) is to simply use a vector<Bullet> rather than vector<Bullet *>. If Bullet is not a concrete class or is a polymorphic base class, it would be better to use some sort of vector<SomethingToManageLifeCycleOfADynamicallyCreatedBullet>. That SomethingToManageLifeCycleOfADynamicallyCreatedBullet might be some sort of smart pointer (but unfortunately cannot be an auto_ptr<Bullet> as std::vector<std::auto_ptr<any_type> > is not allowed by the C++ standard).
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC