阻止copying行为

来源:互联网 发布:大智慧和同花顺知乎 编辑:程序博客网 时间:2024/06/10 02:50

方法一:

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构造函数和赋值操作符};

使用第二种方法最好,在编译期就能报错