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.