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的更多相关文章

  1. 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 ...

  2. 【C++】Item18. Make interfaces easy to use correctly and hard to use incorrectly

    接口容易被正确使用,不易被误用 c++简单工厂模式时,初级实现为ITest* CreateTestOld(), 然后用户负责释放返回的对象.如果忘记释放就会造成memory leak,所以在设计工厂接 ...

  3. 读书笔记 effective c++ Item 18 使接口容易被正确使用,不容易被误用

    1. 什么样的接口才是好的接口 C++中充斥着接口:函数接口,类接口,模板接口.每个接口都是客户同你的代码进行交互的一种方法.假设你正在面对的是一些“讲道理”的人员,这些客户尝试把工作做好,他们希望能 ...

  4. 条款18:让接口容易被正确使用,不易被误用(Make interface easy to use correctly and hard to use incorrectly)

    NOTE : 1.好的接口容易被正确使用,不容易被误用.应该让所有接口努力达成这些性质. 2.“促进正确使用”的办法包括接口的一致性,以及内置类型的行为兼容. 3.“阻止误用”的办法包括建立新类型/限 ...

  5. Effective C++ Item 34 Differentiate between inheritance of interface and inheritance of implementation

    1. 成员函数的接口总是被继承. 如 Item32 所说, public 意味着 is-a, 所以对 base class 为真的任何事情对 derived class 也为真 2. 声明一个 pur ...

  6. 读书笔记 effective c++ Item 31 把文件之间的编译依赖降到最低

    1. 牵一发而动全身 现在开始进入你的C++程序,你对你的类实现做了一个很小的改动.注意,不是接口,只是实现:一个私有的stuff.然后你需要rebuild你的程序,计算着这个build应该几秒钟就足 ...

  7. Effective JavaScript Item 37 认识this的隐式指向

    本系列作为Effective JavaScript的读书笔记. CSV数据通常都会被某种分隔符进行分隔.所以在实现CSV Reader时,须要支持不同的分隔符.那么,非常自然的一种实现就是将分隔符作为 ...

  8. 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问

    1.为什么需要访问资源管理类中的原生资源  资源管理类是很奇妙的.它们是防止资源泄漏的堡垒,没有资源泄漏发生是设计良好的系统的一个基本特征.在一个完美的世界中,你需要依赖这样的类来同资源进行交互,绝不 ...

  9. 读书笔记 effective c++ Item 22 将数据成员声明成private

    我们首先看一下为什么数据成员不应该是public的,然后我们将会看到应用在public数据成员上的论证同样适用于protected成员.最后够得出结论:数据成员应该是private的. 1. 为什么数 ...

随机推荐

  1. 二 、在 JDK 6 and JDK 7中 substring() 方法

    在JDK6 和JDK 7 里面substring(int beginIndex, int endIndex)的方法是不同的.知道这种区别会帮助你更好用它们.为了简单期间,下面用substring() ...

  2. 如何让 Qt 的程序使用 Sleep

    Qt 为何没有提供 Sleep 论坛上不时见到有人问: Qt 为什么没有提供跨平台的 sleep 函数? 使用平台相关的 Sleep 或 nanosleep 以后,界面为什么没有反应? QThread ...

  3. Spring Boot干货系列:(二)配置文件解析

    Spring Boot干货系列:(二)配置文件解析 2017-02-28 嘟嘟MD 嘟爷java超神学堂   前言 上一篇介绍了Spring Boot的入门,知道了Spring Boot使用“习惯优于 ...

  4. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  5. mysql sleep进程 过多

    如果你没有修改过MySQL的配置,缺省情况下,wait_timeout的初始值是28800. wait_timeout过大有弊端,其体现就是MySQL里大量的SLEEP进程无法及时释放,拖累系统性能, ...

  6. DMA2D 图形加速器简介

    在实际使用 LTDC 控制器控制液晶屏时,使 LTDC 正常工作后,往配置好的显存地址写入要显示的像素数据, LTDC 就会把这些数据从显存搬运到液晶面板进行显示,而显示数据的容量非常大,所以我们希望 ...

  7. 【Unity笔记】角色信息常用访问器get/set

    玩家角色Player的实体类演示. // 访问器函数:角色姓名 public string PlayerName { get { return playerName; } set { playerNa ...

  8. 【C#】List<T>对象的深复制

    一.List对象中的T是值类型的情况(int 类型等) 对于值类型的List直接用以下方法就可以复制: List<T> oldList = new List<T>(); old ...

  9. oozie中调度mapreduce

    mapreduce可以直接对hdfs进行清洗和计算,这里介绍oozie中如何调度使用. 操作步骤如下: 1. 写一个mapper和reduce类,并且打包成jar包 2. 在workflow中引用ma ...

  10. linux防火墙开关以及端口规则添加

    1. 重启后生效的 开启: chkconfig iptables on 关闭: chkconfig iptables off 2. 及时生效 开启: service iptables start 关闭 ...