<>05 Functions synthesized automatically by compiler

Normally , If our class doesn't have a custom constructor , copy constructor , And copy operator overloading , Then the compiler will automatically synthesize one of these functions for us . So when you write an empty class , In fact, your code actually contains these functions , But the compiler hides it .
When you need to use these functions , The compiler will synthesize these functions .

Contains references & and const Member variables prevent composite assignments and copies
stay C++ In language , A reference to a variable , Once we have determined that a variable references an object , The reference cannot point to other objects .

And for const Property , We will not be able to assign and modify it , So if a class contains const The compiler will not be able to synthesize the default copy construction and assignment operators for this class , At this time, the user needs to define the corresponding operation .

<>06 Methods of explicitly rejecting compiler composition functions

If the default assignment operator of a base class is set to private, Then all derived classes of this base class will not be able to generate the default assignment operator , Because the assignment operation of each base class should be able to copy the members of the parent class , But the default composition of its parent class is private, So it's impossible to complete the assignment , The compiler will report an error .

therefore , When we need to prevent the compiler from generating the corresponding default function , We can declare the default composition of its base class as private . But it's not completely safe , Because its member functions and friends are still accessible , So while we declare the default composition private, we cannot implement these functions . When you name these functions , The compiler will not be in composition . And when we call these functions with , The compiler will complain .
So as long as we design such a base class , In this way, when our class wants to prevent copying, we can directly use the base class .
C++11 Prevent synthesis in
Under the new standard , We can make it easier to stop copying , In the new standard, we can use operators **=delete To prevent the generation of default composite members , We just need to add after the function we want to block
=delete** that will do .

be careful : Destructors cannot be deleted

Technology