problem with smart pointer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <memory>

using namespace std;

template <class T>
ostream& operator<<(ostream & os,const auto_ptr<T> & p)
{if(p.get() == NULL)
os<<"NULL"<<endl;
else
os<<*p;
return os;
}

int main()
{auto_ptr<int>p(new int(42));
auto_ptr<int>q;

cout<<"After initialization: "<<endl;
cout<<"p: "<<p<<endl;
cout<<"q: "<<q<<endl;

q = p;
cout<<"After assinging auto pointers: "<<endl;
cout<<"p: "<<p<<endl;
cout<<"q: "<<q<<endl;

return 0;
}

when I executes,it appears as
After initialization:
p:42
q:NULL
After assigning auto pointers:
p:42
q:42

assigning smart pointer will transfer the ownership,but it dosen`t`.
My platform is win7,I write the program in vc++ 6.0
Is the matter of my platform or another?
Who can help me.
Thanks.
Last edited on
It's with your platform. VC++ 6.0 is known to be broken and buggy. You should not use it.
Topic archived. No new replies allowed.