There are situations where you don't want to generate copy constructors and copy assignment operators, and you can't just write these functions yourself, because the compiler will automatically generate these functions. It is safe to declare the copy constructor and the copy assignment operator as private. This prevents the compiler from generating the default version and prevents others from calling it.
Note that the "DECLARE member function as private and deliberately do not implement it" is a common tool, even in parts of the standard library.
class Homeforsale// Obviously, the two recipes in the sale are generally different, so the copy constructor and the { // Copy assignment operator are not required. Public : ... Private : ... Homeforsale (const Homeforsale &); // Note that these two functions are only declared operator= (const Homeforsale &);}
But there is also the question of whether the friend function and other member functions might invoke these two functions that actually do not have a bottom definition, so here's another trick: You can have homeforsale inherit from a uncopyable base class:
class uncopyable{ protected: uncopyable () {} ~uncopyable () {} Private: uncopyable (const uncopyable &) {}// prohibit copy. operator(const uncopyable & )}
And let Homeforsale inherit from uncopyable:
class Public uncopyable{ public : ... Private : ...}
Thus, the compiler rejects this operation when the friend or member function of Homeforsale is to invoke the assignment or copy the constructor, because the copy of its base class and the assignment constructor are private. Summary: In order to prohibit the compiler automatically generated some functions, you can declare the corresponding member function is not implemented, using similar uncopyable similar to base class is also a method.
Article 6: If you do not want to use the compiler automatically generated functions, you should explicitly deny.