勤能补拙,厚积薄发

合抱之木,生于毫末;九层之台,起于垒土;千里之行,始于足下
随笔 - 19, 文章 - 0, 评论 - 3, 引用 - 0
数据加载中……

More Effective C++(item10)

prevent resource leaks in constructor
when the resource must be allocated in the constructor,
how to avoid leaks.

using auto_ptr(highly recommended)
 1 #include <string>
 2 #include <list>
 3 #include <memory>
 4 using std::string;
 5 using std::list;
 6 using std::auto_ptr;
 7 
 8 class Image {
 9     public:
10         Image(const string& imageDataFileName);
11         //
12 };
13 
14 class AudioClip {
15     public:
16         AudioClip(const string& audioDataFileName);
17         //
18 };
19 
20 class PhoneNumber {
21     //
22 };
23 
24 class BookEntry {
25     public:
26         // the default arglist can only appear 
27         // where the function is first declared
28         BookEntry(const string& name, 
29                 const string& address = "",
30                 const string& imageFileName = "",
31                 const string& audioClipFileName = "");
32         ~BookEntry() { }
33     private:
34         // attributes
35         string theName; 
36         string theAddress;
37         list<PhoneNumber> thePhones;
38         const auto_ptr<Image> theImage;
39         const auto_ptr<AudioClip> theAudioClip;
40 };
41 
42 BookEntry::BookEntry(const string& name, 
43         const string& address,
44         const string& imageFileName,
45         const string& audioClipFileName)
46 : theName(name), theAddress(""),
47     theImage(imageFileName == "" ? 0:new Image(imageFileName)),
48        theAudioClip(audioClipFileName == "" ? 0:new AudioClip(audioClipFileName))
49 {
50 }

using raw pointer(not very good idea)
 1 #include <string>
 2 #include <list>
 3 using std::string;
 4 using std::list;
 5 class Image {
 6     public:
 7         Image(const string& imageDataFileName);
 8         //
 9 };
10 
11 class AudioClip {
12     public:
13         AudioClip(const string& audioDataFileName);
14         //
15 };
16 
17 class PhoneNumber {
18     //
19 };
20 
21 class BookEntry {
22     public:
23         // the default arglist can only appear 
24         // where the function is first declared
25         BookEntry(const string& name, 
26                 const string& address = "",
27                 const string& imageFileName = "",
28                 const string& audioClipFileName = "");
29         ~BookEntry();
30     private:
31         // release the pointers
32         void cleanup()
33         {
34             delete theImage;
35             delete theAudioClip;
36         }
37         // attributes
38         string theName; 
39         string theAddress;
40         list<PhoneNumber> thePhones;
41         Image *theImage;
42         AudioClip *theAudioClip;
43 };
44 
45 BookEntry::BookEntry(const string& name, 
46         const string& address,
47         const string& imageFileName,
48         const string& audioClipFileName)
49 : theName(name), theAddress(""),
50     theImage(0), theAudioClip(0)
51 {
52     try {
53         if (imageFileName != "") {
54             theImage = new Image(imageFileName);
55         }
56         if (audioClipFileName != "") {
57             theAudioClip = new AudioClip(audioClipFileName);
58         }
59     } catch() {
60         cleanup();
61         throw;
62     }
63 }
64 
65 BookEntry::~BookEntry()
66 {
67     cleanup();
68 }

posted on 2011-11-24 09:14 lee007 阅读(372) 评论(0)  编辑 收藏 引用 所属分类: Programming Study


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理