Effective C++ Item 18 Make interfaces easy to use correctly and hard to use incorrectly
1. A good API will provide easy to use interfaces but also provide hard to miss-use interfaces. Usually the later one is more fundamental than that of the previous one. Consider you want to write a data class, there are thousands ways to write it. Here is one example:
class Date {
public:
Date(int month, int day, int year);
...
};
Date(, , );
Date(, , );
Both are OK, but only one is logical right.
Under such situation, a better way to implement that is to constrict clients what they can do and force them to right direction:
class Month {
public:
static Month Jan() {
return Month();
}
...
private:
explicit Month(int m);
};
Data d(Month::Mar(), Day(), Year());
2. Let's see another example, suppose your interface returns a dynamic allocated resource and will be deleted after all. There are chances that a programmer will forget to delete it or delete it multi times. So, return a smart pointer would be a great idea.
std::tr1::shared_ptr<Investment> createInvestment() {
std::tr1::shared_ptr<Investment> retVal(static_cast<Investment>(),
getRidOfInvestment());
retVal = ...; //let retVal point to right object
return retVal;
}
Besides, smart pointer have another advantage that it will use default or assigned deleter, and you don't need to worry about "cross-DLL problems".
Effective C++ Item 18 Make interfaces easy to use correctly and hard to use incorrectly的更多相关文章
- effective c++ 条款18 make interface easy to use correctly and hard to use incorrectly
举一个容易犯错的例子 class Date { private: int month; int day; int year; public: Date(int month,int day,int ye ...
- 【C++】Item18. Make interfaces easy to use correctly and hard to use incorrectly
接口容易被正确使用,不易被误用 c++简单工厂模式时,初级实现为ITest* CreateTestOld(), 然后用户负责释放返回的对象.如果忘记释放就会造成memory leak,所以在设计工厂接 ...
- 读书笔记 effective c++ Item 18 使接口容易被正确使用,不容易被误用
1. 什么样的接口才是好的接口 C++中充斥着接口:函数接口,类接口,模板接口.每个接口都是客户同你的代码进行交互的一种方法.假设你正在面对的是一些“讲道理”的人员,这些客户尝试把工作做好,他们希望能 ...
- 条款18:让接口容易被正确使用,不易被误用(Make interface easy to use correctly and hard to use incorrectly)
NOTE : 1.好的接口容易被正确使用,不容易被误用.应该让所有接口努力达成这些性质. 2.“促进正确使用”的办法包括接口的一致性,以及内置类型的行为兼容. 3.“阻止误用”的办法包括建立新类型/限 ...
- Effective C++ Item 34 Differentiate between inheritance of interface and inheritance of implementation
1. 成员函数的接口总是被继承. 如 Item32 所说, public 意味着 is-a, 所以对 base class 为真的任何事情对 derived class 也为真 2. 声明一个 pur ...
- 读书笔记 effective c++ Item 31 把文件之间的编译依赖降到最低
1. 牵一发而动全身 现在开始进入你的C++程序,你对你的类实现做了一个很小的改动.注意,不是接口,只是实现:一个私有的stuff.然后你需要rebuild你的程序,计算着这个build应该几秒钟就足 ...
- Effective JavaScript Item 37 认识this的隐式指向
本系列作为Effective JavaScript的读书笔记. CSV数据通常都会被某种分隔符进行分隔.所以在实现CSV Reader时,须要支持不同的分隔符.那么,非常自然的一种实现就是将分隔符作为 ...
- 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问
1.为什么需要访问资源管理类中的原生资源 资源管理类是很奇妙的.它们是防止资源泄漏的堡垒,没有资源泄漏发生是设计良好的系统的一个基本特征.在一个完美的世界中,你需要依赖这样的类来同资源进行交互,绝不 ...
- 读书笔记 effective c++ Item 22 将数据成员声明成private
我们首先看一下为什么数据成员不应该是public的,然后我们将会看到应用在public数据成员上的论证同样适用于protected成员.最后够得出结论:数据成员应该是private的. 1. 为什么数 ...
随机推荐
- 技术blog链接
http://www.cnblogs.com/anrainie/ 蔡羽 基础知识漫谈 http://blog.csdn.net/ioio_jy 姜晔的技术专栏 从苏宁电器到卡巴斯基
- #include <algorithm>中sort的一般用法
1.sort函数的时间复杂度为n*log2(n),执行效率较高. 2.sort函数的形式为sort(first,end,method)//其中第三个参数可选. 3.若为两个参数,则sort的排序默认是 ...
- 基于CSS3自定义发光radiobox单选框
之前我们分享过一些CSS3和HTML5实现的自定义checkbox和Radiobox,比如纯CSS3美化Checkbox和Radiobox按钮,不仅外观唯美,而且Radiobox选中时还有动画效果.今 ...
- 分布式理论(4):Leases 一种解决分布式缓存一致性的高效容错机制(转)
作者:Cary G.Gray and David R. Cheriton 1989 译者:phylips@bmy 2011-5-7 出处:http://duanple.blog.163.com/blo ...
- ubuntu 12.10 默认安装php5-fpm无监听9000端口,nginx无法链接php5-fpm修正
升级php5的时候,发现nginx无法链接到php5,怀疑是php5端口的问题. netstat -an未发现监听9000端口. 查看/var/log/php5-fpm.log一切正常. 随后查看/e ...
- Mysql: Connect/C++ 使用过程中发现返回 std::string 造成的内存泄露
在使用 Connect/C++ ,测试时发现在调用 getString 出现了内存增长的情况. ConstructOutput(); //打印出当前内存 ;i<;++i) { prepareSt ...
- C++很“虚”
0引言:在学习C++时,碰到过以下四个以“虚”命名的概念,在系统理解这些高大上的术语后,才发现它们果真“名不虚传”. 为了方便捋清楚这些概念和之间的相互关系,本人对其进行了系统的总结,欢迎讨论. 1. ...
- Understanding the difficulty of training deep feedforward neural networks
本文作者为:Xavier Glorot与Yoshua Bengio. 本文干了点什么呢? 第一步:探索了不同的激活函数对网络的影响(包括:sigmoid函数,双曲正切函数和softsign y = x ...
- 下列可以用来解析XML的是( )
A.CSS B.DTD C.SAX D.XSL 解答:C java解析xml文件四种方式:SAX DOM JDOM DOM4J
- ffmpeg 源码分析
http://blog.csdn.net/liuhongxiangm/article/details/8824761 https://code.google.com/p/ffmpegsource/is ...