[Negative tive C ++ Reading Notes] 006 _ clause 06 _ if you do not want to use a function automatically generated by the compiler, you should explicitly reject

Source: Internet
Author: User

Preface

Clause 06: if you do not want to use the function automatically generated by the compiler, you should explicitly reject it.

With regard to these terms, I believe that the purpose of doing so is only one thing:

Intention: prevent you from clearly defining the function flow to the customer

Means: Shield C ++ from secretly providing functions for your class

For example, the "=" operator is generated silently by C ++, but you may not want your class to be assigned a value like this. For example, the default constructor, you may not want the user to construct your object as it is.

Implementation Means

Many methods can be used to shield the functions and operator operations that C ++ provides for you. But I also think the method provided by the author is the best. What is the best? Let's look at the sample code!

1 # include <iostream> 2 using namespace STD; 3 4 class uncopyable {5 protected: 6 uncopyable () {} 7 ~ Uncopyable () {} 8 private: 9 uncopyable (const uncopyable &); // use access permission private to solve the problem of blocking default constructor 10 uncopyable & operator = (const uncopyable &); // solve the "=" operator on the screen using the access permission private 11}; 12 13 class myclass: Public uncopyable {14 public: 15 void test () {cout <"myclass" <Endl ;}16}; 17 18 int main () {19 myclass mc1; 20 myclass MC2; 21 22 mc1 = mc2; // error: "uncopyable: Operator =": unable to access private members (declared in the "uncopyable" class) 23 myclass MC3 (mc1); // error: "uncopyable: uncopyable ": private Members cannot be accessed (declared in the "uncopyable" class) 24 25 system ("pause"); 26 return 0; 27}

The coloring part above is the solution provided by the author. I have to say that this trick is indeed quite useful.

Summary

If you want to fully control your class, you need to block the functions and operator operations that C ++ secretly generates for you. The advantage is that there is no clearly defined function flow to the customer.

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.