boost学习之nonecopyable
来源:互联网 发布:tcp服务端口 编辑:程序博客网 时间:2024/06/06 05:49
本文转自:http://blog.csdn.net/huang_xw/article/details/8248960
boost::noncopyable比较简单, 主要用于单例的情况.
通常情况下, 要写一个单例类就要在类的声明把它们的构造函数, 赋值函数, 析构函数, 复制构造函数隐藏到private或者protected之中, 每个类都这么做麻烦.有noncopyable类, 只要让单例类直接继承noncopyable.
class noncopyable的基本思想是把构造函数和析构函数设置protected权限,这样子类可以调用,但是外面的类不能调用,那么当子类需要定义构造函数的时候不至于通不过编译。但是最关键的是noncopyable把复制构造函数和复制赋值函数做成了private,这就意味着除非子类定义自己的copy构造和赋值函数,否则在子类没有定义的情况下,外面的调用者是不能够通过赋值和copy构造等手段来产生一个新的子类对象的。
- #ifndef BOOST_NONCOPYABLE_HPP_INCLUDED
- #define BOOST_NONCOPYABLE_HPP_INCLUDED
- namespace boost {
- // Private copy constructor and copy assignment ensure classes derived from
- // class noncopyable cannot be copied.
- // Contributed by Dave Abrahams
- namespace noncopyable_ // protection from unintended ADL
- {
- class noncopyable
- {
- protected:
- noncopyable() {}
- ~noncopyable() {}
- private: // emphasize the following members are private
- noncopyable( const noncopyable& );
- const noncopyable& operator=( const noncopyable& );
- };
- }
- typedef noncopyable_::noncopyable noncopyable;
- } // namespace boost
- #endif // BOOST_NONCOPYABLE_HPP_INCLUDED
- #include "tfun.h"
- class myclass: public boost::noncopyable
- {
- public:
- myclass(){};
- myclass(int i){};
- };
- int main()
- {
- myclass cl1();
- myclass cl2(1);
- // myclass cl3(cl1); // error
- // myclass cl4(cl2); // error
- return 0;
- }
- boost学习之nonecopyable
- Boost学习之Boost简介
- 【boost学习】之boost::bind
- boost学习之Boost.Lambda
- 学习boost之lexical_cast
- 学习boost之format
- 学习boost之filesystem
- boost学习之function
- boost之spirit学习
- boost学习之-scoped_ptr
- boost学习之-scoped_array
- boost学习之-shared_ptr
- boost学习之-shared_array
- boost学习之-intrusive_ptr
- boost学习之-weak_ptr
- boost学习之-polymorphic_cast
- boost学习之-polymorphic_downcast
- boost学习之-numeric_cast
- activiti5显示流程图
- WARNING: local host name (XXXX) is not qualified如何解决
- Xcode PCBCp Error
- SQL Server 不能通过外部IP访问,解决方法
- TransactionScope使用说明
- boost学习之nonecopyable
- ViewPager使用1-ViewPager多页面滑动切换以及动画效果(ViewPager+Layout+PagerAdapter )
- 第一篇
- 蓝牙Profile的概念和常见种类
- Hibernate(三)—一全面解析多对一关联映射
- 电子购物网站设计—1
- Mysql中的 的 Cascade ,NO ACTION ,Restrict ,SET NULL
- Windows account type 具体描述
- windows7 编译boost1.54