Article 6: If you do not want to use the compiler automatically generated functions, you should explicitly deny.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.