effective c++ 6: Explicitly disallow the use of compiler-generated functions you do not want

假如有个类,我不想让这个类的对象产生副本,即不想让它支持copy构造函数以及copy assignment操作符。那么比较好的办法是把这两个函数声明为private并且不定义

这样即使member或者friend函数想用着两个函数也会发生连接错误。

把这个错误提到编译器的一个好办法是专门设计一个Uncopyable类,然后来继承这个Uncopyable类,这样无论任何形式对这两个函数的调用都会在编译期报错了。

effective c++ 6: Explicitly disallow the use of 
compiler-generated functions you do not want
class Uncopyable {
protected:
    Uncopyable() {}
    ~Uncopyable() {}
private:
    Uncopyable(const Uncopyable&);//阻止copy
    Uncopyable& operator={const Uncopyable&};
};
effective c++ 6: Explicitly disallow the use of 
compiler-generated functions you do not want

有个没弄懂的问题是是否以public来继承这个Uncopyable class。还没太弄明白private继承和public继承,待查。

 

effective c++ 6: Explicitly disallow the use of compiler-generated functions you do not want,布布扣,bubuko.com

effective c++ 6: Explicitly disallow the use of compiler-generated functions you do not want

上一篇:用Robot Framework+python来测试基于socket通讯的C/S系统(网络游戏)


下一篇:C++类模板[转]