Chinaunix首页 | 论坛 | 博客
  • 博客访问: 272076
  • 博文数量: 55
  • 博客积分: 2030
  • 博客等级: 大尉
  • 技术积分: 737
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-13 18:06
文章分类

全部博文(55)

文章存档

2011年(2)

2010年(7)

2009年(17)

2008年(29)

我的朋友

分类:

2008-09-05 01:55:55

智能指针使用:
 

std::tr1::shared_ptr<RectData> pData;

References, pointers, and iterators are all handles

 

class Rectangle {

public:

  ...

  const Point& upperLeft() const { return pData->ulhc; }

  const Point& lowerRight() const { return pData->lrhc; }

  ...

};

dangling handles: handles that refer to parts of objects that don't exist any longer

any function that returns a handle to an internal part of the object is dangerous.

[Item29]

 

void PrettyMenu::changeBackground(std::istream& imgSrc)

{

  Lock ml(&mutex); // from Item 14: acquire mutex and


                                   // ensure its later release


  delete bgImage;

  ++imageChanges;

  bgImage = new Image(imgSrc);

}

it's a good policy not to change the status of an object to indicate that something has happened until something actually has.

"copy and swap." In principle, it's very simple. Make a copy of the object you want to modify, then make all needed changes to the copy. If any of the modifying operations throws an exception, the original object remains unchanged.

Forty years ago, goto-laden code was considered perfectly good practice. Now we strive to write structured control flows. Twenty years ago, globally accessible data was considered perfectly good practice. Now we strive to encapsulate data. Ten years ago, writing functions without thinking about the impact of exceptions was considered perfectly good practice. Now we strive to write exception-safe code.

Time goes on. We live. We learn.

阅读(692) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~