Search code examples
c++pointerssmart-pointersauto-ptr

how this auto_ptr program works and what it does?


I ran this program but I didn't get what this auto_ptr does and on which basics it shows the values?

int main(int argc,char **argv)
{
    int *i= new int;
    auto_ptr<int> x(i);
    auto_ptr<int>y;
    y=x;
    count <<x.get()<<endl;
    count <<y.get()<<endl;
}

Solution

  • This code will print a NULL address for the first auto_ptr object and some non-NULL address for the second, showing that the source object lost the reference during the assignment (=).