auto_ptr assignment

Does anybody know why auto_ptr transfers pointer ownership on assignment instead of making assignment and copy ctor unavailable?
So you can do this?
T obj=*p;
No, that's invoking assignment of type T (assuming auto_ptr<T> p(new T); T obj = *p;)
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.
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
Topic archived. No new replies allowed.