码迷,mamicode.com
首页 > 编程语言 > 详细

effective c++ 条款18 make interface easy to use correctly and hard to use incorrectly

时间:2014-07-12 00:13:54      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   问题   cti   

举一个容易犯错的例子

class Date
{
private:
  int month;
  int day;
  int year;
public:
Date(int month,int day,int year)
{
  this->month = month;
  ...  
}
}

//wrong example
Date date(30,3,1995);//should be 3,30
Date date(2,30,1995);//should be 3,30

使用类型可避免这个问题

class Month
{
  ... 
};
class Day
{
  ... 
};
class Year
{
  ... 
};

class Date
{
private:
  int month;
  int day;
  int year;
public:
Date(Month month,Day day,Year year)
{
  this->month = month;
  ...  
}
}

还有就是给客户资源要尽量自动回收。

参见 std::tr1::shared_ptr<> 

它能指定删除器。

effective c++ 条款18 make interface easy to use correctly and hard to use incorrectly,布布扣,bubuko.com

effective c++ 条款18 make interface easy to use correctly and hard to use incorrectly

标签:style   blog   color   使用   问题   cti   

原文地址:http://www.cnblogs.com/williamwood/p/3832830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!