Consider the following code:
#include
#include
#include
using namespace std;
struct A
{
int a;
A(int
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 delete
s the object and the other still holds a pointer to it.