Negative tive C ++ Clause 6: if you do not want to use a function automatically generated by the compiler, you should explicitly reject

Source: Internet
Author: User

To reject the functions automatically provided by the compiler, the corresponding member functions can be declared as private and not implemented.

If the member function or the friend function calls these functions, an error is reported during the connection.

It is possible to move a connection error to the compilation phase. Set a base classs specifically designed to prevent copying actions:

Class uncopyable {

Protected: // allows derived Object Construction and Analysis

Uncopyable (){}

~ Uncopyable (){}

PRIVATE: // block copying

Uncopyble (const uncopyable &);

Uncopyable & operator = (const uncopyable &);

};

Class homeforsale: Private uncopyable {// class does not declare the copy constructor and the copy assignment operator

};

As long as anyone tries to copy the homeforsale object, the compiler will try to generate a copy constructor and a copy assignment operator. The Compiler version of these functions will try to call the corresponding brother of their base class, those calls will be rejected by the compiler because the copy function of the base class is private.

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.