Possible Duplicate:
Why is it wrong to use std::auto_ptr<> with standard containers?
I have a function to return some Object in pointer, so I use:
vector <auto_ptr <Object> > func() { ... }
I want to know whether it is safe or not?
Possible Duplicate:
Why is it wrong to use std::auto_ptr<> with standard containers?
I have a function to return some Object in pointer, so I use:
vector <auto_ptr <Object> > func() { ... }
I want to know whether it is safe or not?
stl likes to copy elements around and use temporary copies in its algorithms (e.i. they have to be "copy-constructible" and "assignable"). While this is not true for auto_ptr
. Assigning one auto_ptr
to another transfers ownership of the pointer.
auto_ptr<foo> A = B;
B becomes NULL
, A is the new owner of the pointer and B is unusable. And when the temporary object takes ownership it does not return it and you have vector of dangling pointer or NULL
pointers instead.
You can use c++11 smart pointer, or boost smart pointers or implement one yourself.