 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
berthelot samuel Guest
|
Posted: Wed Jul 30, 2003 8:46 am Post subject: vectors stl with pointers |
|
|
hi all,
I have a simple question. I use a stl vector of pointers on objects of
my own class, say Person:
vector<Person*> vectPerson;
vectPerson.push_back(new Person("Sam"));
when I call vectPerson.pop_back, do i need to take care of the pointer
by calling delete or does the pop method handle this for me ?
thx
Sam
|
|
| Back to top |
|
 |
Patrik Stellmann Guest
|
Posted: Wed Jul 30, 2003 9:22 am Post subject: Re: vectors stl with pointers |
|
|
| Quote: | when I call vectPerson.pop_back, do i need to take care of the pointer
by calling delete or does the pop method handle this for me ?
you will have to delete it on your own! |
|
|
| Back to top |
|
 |
Peter van Merkerk Guest
|
Posted: Wed Jul 30, 2003 9:22 am Post subject: Re: vectors stl with pointers |
|
|
"berthelot samuel" <samuel.berthelot (AT) voila (DOT) fr> wrote:
| Quote: | I have a simple question. I use a stl vector of pointers on objects of
my own class, say Person:
vector<Person*> vectPerson;
vectPerson.push_back(new Person("Sam"));
when I call vectPerson.pop_back, do i need to take care of the pointer
by calling delete or does the pop method handle this for me ?
|
No, you will have to do it yourself. The pop methods just remove the
pointer from the vector. The std::vector class cannot know if it has to
delete the object that is being referenced by the pointer; depending on
the situation it may or may not be the appropriate thing to do. Rather
than using raw pointers, you may also opt for putting boost::shared_ptr
of the boost library (http://www.boost.org/) in the vector. This way you
don't have to worry about ownership issues.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Wed Jul 30, 2003 12:01 pm Post subject: Re: vectors stl with pointers |
|
|
"berthelot samuel" <samuel.berthelot (AT) voila (DOT) fr> wrote
| Quote: | hi all,
I have a simple question. I use a stl vector of pointers on objects of
my own class, say Person:
vector<Person*> vectPerson;
vectPerson.push_back(new Person("Sam"));
when I call vectPerson.pop_back, do i need to take care of the pointer
by calling delete or does the pop method handle this for me ?
thx
Sam
|
No you have to do it one your own, also you have to keep track of copies of
your vector.
This is why you shouldn't use raw pointers in an STL class (or anywhere
similar). Do yourself a favour and learn about smart pointers, will save you
months of debugging tricky pointer problems. Have a look at the smart_ptr
class from www.boost.org for instance, or get a good book.
john
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|