problem with auto_ptr



Hello All ..

i am getting strange errors when trying following code . any idea ?

i am using vc++ 2008

thanks in advance.

1
2
3
4
5
vector<auto_ptr<int> > vx;

auto_ptr<int> xp( new int) ;

vx.push_back(xp);
You can't store a std::auto_ptr<> in stl containers. I think that this is because it has non-const copy semantics.
you aren't supposed to use them in containers.

an object may be safely pointed to by only one
auto_ptr, so copying an auto_ptr copies the pointer and
transfers ownership to the destination if the source had
already had ownership.


why use pointers? you aren't gaining any space or speed
by using a pointer over a bare int.
the container makes a copy anyway, and copying
a pointer will cost the same.

if you must, use boost::shared_ptr

Last edited on


Thanks To all. boost will solve my problem.

Topic archived. No new replies allowed.