C++ 编写一个不可复制的类

C++ 编写一个不可复制的类

flyfish

Effective C++:条款06
若不想使用编译器自动生成的函数,就该明确拒绝 .
Explicitly disallow the use of complier-generated functions you do not want.


老式C++的写法

class noncopyable
{
protected:
	noncopyable() {}
	~noncopyable() {}
private:
	noncopyable( const noncopyable& );
	noncopyable& operator=( const noncopyable& );
};


C++11的写法
class noncopyable
{
protected:
	constexpr noncopyable() = default;
	~noncopyable() = default;
	noncopyable( const noncopyable& ) = delete;
	noncopyable& operator=( const noncopyable& ) = delete;
};


Boost的实现
Boost不将两种方法结合,还防止无意识的ADL(protection from unintended ADL)
ADL(Argument Dependent Lookup)


	namespace boost {
		namespace noncopyable_ 
		{
			class noncopyable
			{


			};
		}
		typedef noncopyable_::noncopyable noncopyable;


} 


你可能感兴趣的:(boost,noncopyable)