接口容易被正确使用,不易被误用

c++简单工厂模式时,初级实现为ITest* CreateTestOld(), 然后用户负责释放返回的对象。如果忘记释放就会造成memory leak,所以在设计工厂接口时就应屏蔽这个潜在的问题,这时就可以用智能指针shared_ptr<ITest> CreateTest(),由他负责对象资源的管理,而对客户端的使用来说更简洁了。

 #include "stdafx.h"
#include <memory>
#include <iostream>
using namespace std; class ITest
{
public:
virtual void Func() = ;
virtual ~ITest(){}
}; class Test : public ITest
{
public:
void Func() override
{
cout << "Test::Func" << endl;
}
Test()
{
cout << "Test ctor!" << endl;
}
~Test()
{
cout << "Test destroy!" << endl;
} }; class Factory
{
public:
static shared_ptr<ITest> CreateTest()
{
return shared_ptr<ITest>(new Test);
} static ITest* CreateTestOld()
{
return new Test;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
auto ptr2 = Factory::CreateTestOld();
ptr2->Func();
delete ptr2; auto ptr = Factory::CreateTest();
ptr->Func(); return ;
}

【C++】Item18. Make interfaces easy to use correctly and hard to use incorrectly的更多相关文章

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

  2. 【bzoj2751】[HAOI2012]容易题(easy) 数论-快速幂

    [bzoj2751][HAOI2012]容易题(easy) 先考虑k=0的情况 那么第一个元素可能为[1,n] 如果序列长度为m-1时的答案是ans[m-1] 那么合并得 然后同理答案就是 k很小 而 ...

  3. 【AtCoder】AGC005 F - Many Easy Problems 排列组合+NTT

    [题目]F - Many Easy Problems [题意]给定n个点的树,定义S为大小为k的点集,则f(S)为最小的包含点集S的连通块大小,求k=1~n时的所有点集f(S)的和取模92484403 ...

  4. 【CodeForces】913 D. Too Easy Problems

    [题目]D. Too Easy Problems [题意]给定n个问题和总时限T,每个问题给定时间ti和限制ai,当解决的问题数k<=ai时问题有效,求在时限T内选择一些问题解决的最大有效问题数 ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?

    UVa11991 Easy Problem from Rujia Liu?  思路:  构造数组data,使满足data[v][k]为第k个v的下标.因为不是每一个整数都会出现因此用到map,又因为每 ...

  7. 【转载】【翻译】Breaking things is easy///机器学习中安全与隐私问题(对抗性攻击)

    原文:Breaking things is easy 译文:机器学习中安全与隐私问题(对抗性攻击) 我是通过Infaraway的那篇博文才发现cleverhans-blog的博客的,这是一个很有意思的 ...

  8. 【练习】reserving.kr之easy ELF

    打开主函数: int __cdecl main() { int result; // eax@2 write(, "Reversing.Kr Easy ELF\n\n", 0x17 ...

  9. 【bzoj2751】[HAOI2012]容易题(easy) 数论,简单题

    Description 为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下:有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些A[i]不能取哪 ...

随机推荐

  1. Oldboy 基于Linux的C/C++自动化开发---MYSQL

    http://www.eimhe.com/forum.php?mod=viewthread&tid=142952#lastpost http://www.eimhe.com/thread-14 ...

  2. mysql利用timestamp来进行帖子排序

    select * from table order by timestamp descorder by 该列 desc timestamp字段也可以用来排序,对应Java类型的.net.timesta ...

  3. 纤程(FIBER)

    Indy 10 还包含对纤程的支持.纤程是什么?简单来说,它也是 一个“线程”,但是它是由代码控制的,而不是由操作系统控制的.实际上,可以认为线程 是一个高级纤程.纤程和 Unix 用户线程(Unix ...

  4. 条款十五: 让operator=返回*this的引用

    c++程序员经常犯的一个错误是让operator=返回void,这好象没什么不合理的,但它妨碍了连续(链式)赋值操作,所以不要这样做. 一般情况下几乎总要遵循operator=输入和返回的都是类对象的 ...

  5. man中文手册

    Ubuntu安装man手册 sudo apt-get install manpages-zh CentOS安装man手册 yum install man man中文安装包 yum install ma ...

  6. java 数据结构. 源代码阅读

    Collections工具类里的 Collections.synchronizedList public static <T> List<T> synchronizedList ...

  7. 一个python自动化测试的例子

    http://blog.csdn.net/galen2016/article/details/70882483 https://www.cnblogs.com/TankXiao/category/47 ...

  8. &times变成x

    昨天晚上遇到一个很尴尬的bug. 当使用IE浏览器,跳转链接使用&传参的时候第二个参数是times,也就是&times(你有可能看到的是x,实际是&times),结果& ...

  9. python 简单连接mysql数据库

    1. 安装pymysql 库 pip install pymysql 2.实例本地连接mysql库 #!/usr/bin/python # encoding: utf-8 ""&q ...

  10. cocos2dx 在android平台打开文件问题

        我们有一个项目是基于cocos2dx + lua,在网络部分用到了protobuf, 在初始化protobuf的时候须要读取本地文件,用lua的io.open读取文件在windows,ios上 ...