More effective C + +: preventing resource leaks

Source: Internet
Author: User
Tags resource

If you are developing an address book program with a multimedia function. In addition to storing the usual text information such as name, address, telephone number, this address book can also store photos and sounds (give them the correct pronunciation of their names).

To implement this communication, you can design this:

class Image { // 用于图像数据
 public:
  Image(const string& imageDataFileName);
  ...
};
class AudioClip { // 用于声音数据
 public:
  AudioClip(const string& audioDataFileName);
  ...
};
class PhoneNumber { ... }; // 用于存储电话号码
class BookEntry { // 通讯录中的条目
 public:
  BookEntry(const string& name,
  const string& address = "",
  const string& imageFileName = "",
  const string& audioClipFileName = "");
  ~BookEntry();
  // 通过这个函数加入电话号码
  void addPhoneNumber(const PhoneNumber& number);
  ...
 private:
  string theName; // 人的姓名
  string theAddress; // 他们的地址
  list thePhones; // 他的电话号码
  Image *theImage; // 他们的图像
  AudioClip *theAudioClip; // 他们的一段声音片段
};

Each entry in the Address Book has a name data, so you need a constructor with a parameter (see clause 3), but other content (address, image, and sound file names) are optional. Note You should use a Chain list class (list) to store the phone number, which is a container class (container classes) in the standard C + + class library (STL). (see effective C + + clause 49 and the terms of this book 35)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.