http://blog.csdn.net/huang_xw/article/details/8248960#

boost::noncopyable比较简单, 主要用于单例的情况.
通常情况下, 要写一个单例类就要在类的声明把它们的构造函数赋值函数析构函数, 复制构造函数隐藏到private或者protected之中, 每个类都这么做麻烦.
有noncopyable类, 只要让单例类直接继承noncopyable. 
class noncopyable的基本思想是把构造函数和析构函数设置protected权限,这样子类可以调用,但是外面的类不能调用,那么当子类需要定义构造函数的时候不至于通不过编译。但是最关键的是noncopyable把复制构造函数和复制赋值函数做成了private,这就意味着除非子类定义自己的copy构造和赋值函数,否则在子类没有定义的情况下,外面的调用者是不能够通过赋值和copy构造等手段来产生一个新的子类对象的。

  1. #ifndef BOOST_NONCOPYABLE_HPP_INCLUDED
  2. #define BOOST_NONCOPYABLE_HPP_INCLUDED
  3.  
  4. namespace boost {
  5.  
  6. // Private copy constructor and copy assignment ensure classes derived from
  7. // class noncopyable cannot be copied.
  8.  
  9. // Contributed by Dave Abrahams
  10.  
  11. namespace noncopyable_ // protection from unintended ADL
  12. {
  13. class noncopyable
  14. {
  15. protected:
  16. noncopyable() {}
  17. ~noncopyable() {}
  18. private: // emphasize the following members are private
  19. noncopyable( const noncopyable& );
  20. const noncopyable& operator=( const noncopyable& );
  21. };
  22. }
  23.  
  24. typedef noncopyable_::noncopyable noncopyable;
  25.  
  26. } // namespace boost
  27.  
  28. #endif // BOOST_NONCOPYABLE_HPP_INCLUDED
  1. #include "tfun.h"
  2.  
  3. class myclass: public boost::noncopyable
  4. {
  5. public:
  6. myclass(){};
  7. myclass(int i){};
  8. };
  9.  
  10. int main()
  11. {
  12. myclass cl1();
  13. myclass cl2();
  14.  
  15. // myclass cl3(cl1); // error
  16. // myclass cl4(cl2); // error
  17.  
  18. return ;
  19. }

boost::noncopyable介绍的更多相关文章

  1. boost::bind 介绍

    boost::bind 介绍   这篇文章介绍boost::bind()的用法, 文章的主要内容是参考boost的文档. 1. 目的 boost::bind 是std::bindlist 和 std: ...

  2. Boost noncopyable实现禁止拷贝的类

    在C++中定义一个类,如果不明确定义拷贝构造函数和拷贝赋值操作符,编译期会为我们自动生成这两个函数.但是我们有时又希望禁止拷贝类的实例,这时可以私有化拷贝构造函数和拷贝赋值操作符即可. class d ...

  3. boost noncopyable类

    1. 当声明一个类时,编译器会自动为该类生成默认构造函数,复制构造函数,赋值操作符以及析构函数: 2.自动生成的各个函数和操作符都是public的: 3.当声明一个类不允许复制时,可以将一个类的复制构 ...

  4. boost::noncopyable

    /** * boost::noncopyable 实现单例不用麻烦了,直接从这个继承就行了 */ #include <boost/noncopyable.hpp> class myclas ...

  5. Boost Asio介绍--之一

    原文:http://www.tuicool.com/articles/YbeYR3 Boost Asio介绍--之一 时间 2014-03-26 17:57:39  CSDN博客 原文  http:/ ...

  6. boost::function和boost::bind 介绍

    一. boost::function介绍 原文:http://www.cnblogs.com/sld666666/archive/2010/12/16/1907591.html 本片文章主要介绍boo ...

  7. (转)boost::bind介绍

    转自:http://www.cnblogs.com/sld666666/archive/2010/12/14/1905980.html 这篇文章介绍boost::bind()的用法, 文章的主要内容是 ...

  8. boost::function 介绍

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

  9. Boost总结汇总

    从开始接触Boost已经有好几年了,而对它的掌握却难言熟悉,有对它部分的源代码的剖析也是蜻蜓点水.有时间一点点梳理一下吧. 1. 概述 [Boost]C++ Boost库简介[Boost]C++ Bo ...

随机推荐

  1. e835. 使JTabbedPane中的卡片生效和失效

    By default, all new tabs are enabled, which means the user can select them. A tab can be disabled to ...

  2. e616. Determining If a Focus Lost Is Temporary or Permanent

    A temporary focus-lost event occurs if the focus moves to another window. It's temporary because the ...

  3. javascript -- canvas绘制曲线

    绘制曲线有几种思路: 1.通过quadraticCurveTo(controlX, controlY, endX, endY)方法来绘制二次曲线 2.通过bezierCurveTo(controlX1 ...

  4. XAMPP permissions on Mac OS X

    $ cd /Applications $ XAMPP/ 注意: 改变的是XAMPP目录,而不是htdocs ref: http://stackoverflow.com/questions/904697 ...

  5. t4 根据表名数组生成实体

    <#@ template debug="false" hostspecific="true" language="C#" #> ...

  6. linux中crontab命令

    一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...

  7. Java常用类(二)String类详解

    前言 在我们开发中经常会用到很多的常用的工具类,这里做一个总结.他们有很多的方法都是我们经常要用到的.所以我们一定要把它好好的掌握起来! 一.String简介 1.1.String(字符串常量)概述 ...

  8. android studio怎么导入appcompat-v7

    方法1: 在项目的build.gradle文件里,用传统的自动依赖处理方式:compile ‘com.Android.support:appcompat-v7:+’,然后rebuild就可以了. 方法 ...

  9. js sendBeacon

    页面性能日志: DNS解析耗时 TCP链接耗时 SSL安全链接耗时 网络请求耗时 DOM解析耗时 资源加载耗时 首包时间 白屏时间 首次可交换时间 Dom Ready时间 页面完全加载时间. 如某些统 ...

  10. bat 变量作用域

    set answer=one if true equ true ( set answer=two echo %answer% ) echo Argument is %answer% pause