Effective C + + clause 6 If you do not want to use a function generated automatically by the compiler, you should explicitly deny

Source: Internet
Author: User

1. The meaning of some classes determines that they do not have certain functions, that is, some functions can not be created to prevent the wrong use (for example, define a book class, it contains a variable that represents the ISBN, in which case the copy constructor and the assignment operator is obviously meaningless, Because the ISBN for any two books is different), the compiler produces these functions in cases where the creator of the class does not declare a default constructor, copy constructors, assignment operators, and destructors, in order to avoid this situation, These functions can be declared private and do not provide their definitions to prevent their use, but if these functions are called through other member functions or friend functions, the error will be found in the link period (the definition and use of the class are usually in different files).

2. In order to advance the link-period error to the compile time, you can set up a base class that contains only the declarations of these functions and set it to private, and then inherit it with the target class. The downside is that the inefficiency of inheritance and the emergence of multiple inheritance can prevent the "empty base class Optimization ".

Effective C + + clause 6 If you do not want to use a function generated automatically by the compiler, 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.