1 #include <iostream>
2 #include <memory>
3
4 using namespace std;
5
6 template <class T>
7 ostream& operator<<(ostream& strm, const auto_ptr<T>& p) {
8 // does p own a object?
9 if(p.get() == NULL) {
10 strm << "NULL";
11 } else {
12 strm << *p;
13 }
14 return strm;
15 }
16
17 int main() {
18 auto_ptr<int> p(new int(42));
19 auto_ptr<int> q;
20
21 cout << "after initialization: " << endl;
22 cout << "p: " << p << endl;
23 cout << "q: " << q << endl;
24
25 q = p;
26 cout << "after assigning auto pointers" << endl;
27 cout << "p: " << p << endl;
28 cout << "q: " << q << endl;
29
30 *q += 13;
31 p = q;
32 cout << "after change and reassignment" << endl;
33 cout << "p: " << p << endl;
34 cout << "q: " << q << endl;
35
36 return 0;
37 }