More Effective C++(item10)

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 }

你可能感兴趣的:(More Effective C++(item10))