方法一:
class Derive
{
private:
Derive(const Derive&);
Derive& operator=(const Derive&); //只声明,不定义
};
方法二:
class Uncopyable
{
protected:
Uncopyable() {} //允许Derived对象构造和析构
~Uncopyable() {}
private:
Uncopyable(const Uncopyable&); // 阻止copying行为
Uncopyable& operator=(const Uncopyable&);
};
class Derive: private Uncopyable
{
//不再定义 copy构造函数和赋值操作符
};
使用第二种方法最好,在编译期就能报错