Effective C++ 笔记 —— Item 6: Explicitly disallow the use of compiler-generated functions you do not want.
To disallow functionality automatically provided by compilers:
1. declare the corresponding member functions private and give no implementations.
class HomeForSale { public: //... private: //... HomeForSale(const HomeForSale&); // declarations only HomeForSale& operator=(const HomeForSale&); };
2. Using a base class like Uncopyable is one way to do this
class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects... private: Uncopyable(const Uncopyable&); // ...but prevent copying Uncopyable& operator=(const Uncopyable&); }; /*To keep HomeForSale objects from being copied, all we have to do now is inherit from Uncopyable :*/ class HomeForSale : private Uncopyable { // class no longer declares copy ctor or copy assign. operator };