Item 6. Explicitly disallow the use of compile-generated functions you do not want.

本篇条目介绍了如何防止编译器调用其自动创建的函数(item5中提到的4种函数)。

Usually, if you don't want a class to support a particular kind of functionality, you simply don't declare the function that would provide it. But sometimes as the Item 5 points out,  if you don't declare them and somebody tries to call them, compilers declare them for you.

Here are some ways to prevent compiler generated functions:
The key to the solution is that all the compiler generated functions are public. To prevent these functions from being generated, you must declare them yourself, but there is nothing that requires that you declare them public. Instead, declare the copy constructor and the copy assignment operator private. By declaring a member function explicitly, you prevent compilers from generating their own version, and by making the function private, you keep people from calling it.
But member and friend function can still call this private functions. So, another trick is declaring member functions private and deliberately not implementing them. 
     class NonCom{public:.....private:NonCom(const NonCom&);NonCom& operator=(const NonCom&);...};

If you inadvertently try to do call this function in a member or a friend function, the linker will complain.

It's possible to move the link- time error up to compile time (always a good thing—earlier error detection is better than later) by declaring the copy constructor and copy assignment operator private  not in NonCom itself, but in a base class specifically designed to prevent copying. The base class is simplicity itself:
     class noncopyable{public:noncopyable(){}~noncopyable(){}private:noncopyable(const noncopyable&);noncopyable& operator=(const noncopyable&);};class NonCom : noncopyable{...}; 

This works, because compilers will try to generate a copy constructor and a copy assignment operator if anybody — even a member or friend function — tries to copy a NonCom object. As Item 12 (See 10.8) explains, the compiler- generated versions of these functions will try to call their base class counterparts, and those calls will be rejected, because the copying operations are private in the base class.

Things to Remember

  • To disallow functionality automatically provided by compilers, declare the corresponding member functions private and give no implementations. Using a base class like Uncopyable  is one way to do this.

《Effective C++》读书笔之六 Item 6. Explicitly disallow the use of compile-generated functions相关推荐

  1. Effective C++记录(6):Explicitly disallow the use of compiler-generated functions you do not want

    若不想使用编译器自动生成的函数,就该明确拒绝 当一个对象的存在就是独一无二的,设计之初该类就不允许被拷贝赋值,此时就应该处理该类中拷贝构造以及拷贝赋值操作符. 方法: 1. 将拷贝构造以及拷贝赋值操作 ...

  2. Effective C++读书笔记 摘自 pandawuwyj的专栏

    Effective C++读书笔记(0)       Start   声明式(Declaration):告诉编译器某个东西的名称和类型,但略去细节.   std::size_t numDigits(i ...

  3. more effective c++和effective c++读书笔记

    转载自http://bellgrade.blog.163.com/blog/static/83155959200863113228254/,方便日后自己查阅, More Effective C++读书 ...

  4. java pda开发_《Java 手机/PDA 程序设计入门》读书笔

    JDBC针对桌面平台或企业用户设计,不适合移动通讯设备. MIDP为的永久性数据储存提供了面向纪录的数据库(Record Orieted Database),称作"记录管理系统"R ...

  5. Beginning WF4读书笔计 - 第一章 03设计示图及xaml代码

    通过<Beginning WF4读书笔计 - 第一章 01创建一个简单的工作流>和<Beginning WF4读书笔计 - 第一章 02添加程序元素>的介绍后,本示例基本完成. ...

  6. Effective Java读书笔记(二)

    Effective Java 读书笔记 (二) 创建和销毁对象 遇到多个构造器参数时要考虑使用构建器 创建和销毁对象 何时以及如何创建对象? 何时以及如何避免创建对象? 如何确保它们能够适时地销毁? ...

  7. Maltab在数学建模中的应用(第二版)——读书笔记下

    Maltab在数学建模中的应用(第二版)--读书笔记下 1彩票中的数学2002B 总结 2露天卡车调度问题2003B 总结 3奥运会商圈规划问题2004A 总结 4交巡警服务平台的设置与调度2011B ...

  8. Effective STL 读书笔记

    Effective STL 读书笔记 标签(空格分隔): 未分类 慎重选择容器类型 标准STL序列容器: vector.string.deque和list(双向列表). 标准STL管理容器: set. ...

  9. Effective Java 读书笔记(七):通用程序设计

    Effective Java 读书笔记七通用程序设计 将局部变量的作用域最小化 for-each 循环优于传统的 for 循环 了解和使用类库 如果需要精确的答案请避免使用 float 和 doubl ...

最新文章

  1. 从自来水公司到社会工作者再做运维,我深耕 IT 一线的这 8 年
  2. Spring MVC 拦截器 interceptor 详解
  3. 鸟哥的Linux私房菜(基础篇)- 第二十六章、Linux 核心编译与管理
  4. DAY3-Flask项目
  5. c# 标准正太分布函数_数据标准化处理中的min-max和zscore
  6. 深入学习二叉树(三) 霍夫曼树
  7. spark中dataframe解析_SparkSql 中 JOIN的实现
  8. 案例演示按角色的form认证实现过程
  9. 7-1 射击游戏 (20 分)
  10. Git学习系列之如何正确且高效地将本地项目上传到Github(图文详解)
  11. C#版Windows服务安装卸载小工具-附源码
  12. 计算机基础98均9,第三章 计算机基础 Windows98 (第二讲).ppt
  13. HTML 5 会为 Flash 和 Silverlight 送终吗?
  14. Chrome插件安装 程序包无效
  15. 电气技术与计算机技术结合,探析计算机与电气自动化技术有机结合.doc
  16. AiTrust下预训练和小样本学习在中文医疗信息处理挑战榜CBLUE表现
  17. 0501 0503 模块区别_深圳兼容思科光模块市场
  18. Codevs 侦探推理
  19. MySQL学习笔记--常用存储引擎InnoDB与MyISAM总结
  20. Java对象内存布局(JOL)

热门文章

  1. 《新概念》英语的学习方法
  2. 柯达四年后彻底退出胶卷业务
  3. 5、店铺管理 - 后端功能开发 - 微擎小程序模块应用开发
  4. 全国计算机竞赛能保送清华北大吗,NOIP考试是什么?能保送清华北大是真的吗?...
  5. 程序员特有的拜年祝词
  6. 快车道不快的现象与人类误判心理学
  7. iOS设置键盘上Return按键及点击操作
  8. 福布斯发布2019年度全球亿万富豪榜:贝佐斯蝉联首富 马化腾马云上榜
  9. Latex 中目录生成超链接
  10. 征女友(搞笑)(转)