Oct 15, 2009 at 9:03pm UTC
Does anybody know why auto_ptr transfers pointer ownership on assignment instead of making assignment and copy ctor unavailable?
Oct 15, 2009 at 9:10pm UTC
So you can do this?
T obj=*p;
Oct 15, 2009 at 9:47pm UTC
No, that's invoking assignment of type T (assuming auto_ptr<T> p(new T); T obj = *p;)
Oct 15, 2009 at 9:51pm UTC
Oh, then your question is why you can assign an auto_ptr to another at all?
Well, a pointer that can't be copied isn't very useful. It would be no different than an object created on the stack.
Oct 16, 2009 at 1:09am UTC
Because you might want to transfer the assignment like this maybe:
1 2 3 4 5 6 7 8 9
std::auto_ptr<int > foo(int data) {
return std::auto_ptr<int > (new int (data));
}
int main() {
std::auto_ptr<int > ptr(NULL);
ptr = foo(25); //need = on here
//...
}
Last edited on Oct 16, 2009 at 1:09am UTC