![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Posts: 23
Rep Power: 0
![]() |
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, |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
You can't remove the vector like this, you can deallocate its content though, using:
bulletList.clear(); |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 884
Rep Power: 4
![]() |
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(); |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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).
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|