Ray's playground

 

Utilities(Chapter 4 of The C++ Stardard Library)

 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 {
 9     if(p.get() == NULL)
10     {
11         strm << "NULL";
12     }
13     else
14     {
15         strm << *p;
16     }
17     
18     return strm;
19 }
20 
21 int main()
22 {
23     auto_ptr<int> p(new int(42));
24     auto_ptr<int> q;
25 
26     cout << "after initialization:" << endl;
27     cout << " p: " << p << endl;
28     cout << " q: " << q << endl;
29 
30     q = p;
31     cout << "after assignment" << endl;
32     cout << " p: " << p << endl;
33     cout << " q: " << q << endl;
34 
35     *+= 13;
36     p = q;
37     cout << "after change and reassignment" << endl;
38     cout << " p: " << p << endl;
39     cout << " q: " << q << endl;
40 
41     cin.get();
42 }
43 

 

 

posted on 2009-11-17 12:29  Ray Z  阅读(289)  评论(0编辑  收藏  举报

导航