Compilation problems with vector<auto_ptr<> >

假如想象 提交于 2019-11-29 10:22:16
Joel

Correct, std::auto_ptr cannot be used in std::vector.

What the compiler is complaining about there is the assignment operator for auto_ptr changes the object being assigned from, and so it cannot be const.

You want to use either boost::ptr_vector or a vector of boost::shared_ptrs

auto_ptr has a copy constructor with a non-const parameter, so the compiler can't call it from vector::push_back() since the latter has const parameter.

The reason is when you initialize one auto_ptr instance from another the new instance disconnects the object from the other instance and connects it to self so avoid a dangling pointer situation when one instance deletes the object and the other still holds a pointer to it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!