boost/noncopyable.hpp

boost/noncopyable.hpp

#include <boost/noncopyable.hpp>

 

noncopyable类阻止派生类拷贝构造和赋值构造。

就是把拷贝构造函数和赋值构造函数,置于保护。当一个类定义时,如果不指定,那会有默认的拷贝和赋值构造函数,这种默认函数在某些操作时会产生严重的后果。直接指定这些复制操作,可以减少语义错误。noncopyable的实现就是把拷贝和赋值构造申明为私有。

//  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


你可能感兴趣的:(boost/noncopyable.hpp)