13th Chapter Copy Control
1. What is a copy constructor ? P440
If the first parameter of a class's constructor is a reference to its own class type, and all other parameters have default values, then this is a copy constructor.
2. Copy initialization and direct initialization. P441
If you initialize an object with an equal sign "=" when initializing, it is copy initialization .
Conversely, if you initialize without an equal sign "=", you are initializing directly .
In the case of copy initialization , it must be a copy constructor (and possibly a move constructor).
In the case of direct initialization , the constructor corresponding to the Parameter form is used, but it is also possible to copy the constructor function.
3. Copy constructors occur not only when we define variables with =, but also in:
Passes an argument object to a non-reference parameter.
Returns the object as a non-reference return value. ( However, if a new object continues to be initialized with the returned object , The procedure will not call the copy constructor. )
Initializes an element of an array or a member of an aggregation class with a list of curly braces.
Objects such as vectors are initialized with curly braces or when using push_back and insert operations.
The above B.V was supposed to be 31, but it was 21. This should be where the compiler proactively skips over the copy constructor when initializing B, so if we want to define a copy constructor for a class , We define its behavior correctly . Do not add some behavior that is inherently not a copy, as in the above example. Because you can't. 100% guarantee that the copy constructor can be executed several times ...
4. Why does the class object parameter of the copy constructor have to be a reference type?
Assume that the class object passed by the copy constructor is not a reference. Now to pass the argument B to the non-reference parameter A, then a calls its own copy constructor to construct a through B. But the copy constructor of a also requires a non-referenced class object to do the formal parameter, so
The formal parameters of a's copy constructor begin to call their own copy constructors to construct themselves through B ... And so on, infinite loops.
5. Copy the assignment operator. P443
The copy assignment operator is the "=" operator when assigning a value to another class object using one Class object (not "=" when initializing ).
6. A class object with a pointer type member uses the copy assignment operator of the composition.
You can see that pointer members of a and B objects point to the same address, and when they are refactored, the latter destructor deletes an invalid pointer.
7. Use =default. P449
Using =default can show that the compiler is required to synthesize copy control members for us.
8. Block the copy. P449
You can block a copy by adding =delete after the copy constructor and copy assignment operators. Why do you want to block copies? For example, the iostream class blocks the copy to prevent multiple objects from writing to or reading the same IO cache.
9. Prior to C++11, a copy ( or copy initialization ) of the class object was prevented by declaring the copy constructor and copy assignment operator of the classes private and undefined . Even friends cannot copy the type because it is undefined.
10. Behaves like a worthy class. P453
You must define your own: default constructor , copy constructor , copy assignment operator, and destructor .
and the copy assignment operator must be able to handle the self-assignment .
11. Define a class that behaves like a smart pointer . P455
( the first sentence + + (*num) in the copy assignment operator of class a in the following code should be changed to + + (*rhs.num) )
12. Define your own swap function for the class that gives the value behavior. P458
13. Lvalue Reference and Rvalue reference: P471
The left value is persistent and the right value is short.
A variable is an lvalue, so you cannot bind an rvalue reference to a variable. Even if the variable itself is an rvalue reference.
14. Move the constructor with the move assignment operator. P473
The essence of moving a constructor is actually capturing an object's memory resource directly to the object being constructed, and the original object cannot continue to control the memory.
Synthesis of mobile operation P475
Only one class does not define any copy control members ( copy Constructors , copy assignment Operators , destructors ), and all non-static members of the class are movable, The compiler will now give the class a composite move constructor and move assignment operator.
16. What is used when there is a move operation for the existing copy operation? P477
a rule : Move Right value , Copy left value .
That is, when the right side is a right value, the move operation ( or copy ) is used preferentially.
When the right side is an lvalue, only the copy operation can be used (unless you convert it using std::move )
Note the above Get The result returned by the function is a right value . and if we define Get as follows :
A get (a a)
{
return A;
}
a copy constructor is used at the time the parameter is passed , which adds "111" to the value of a.
17. If there is no move operation, only the copy operation, then the right value is also copied. P477
If necessary, the right value can also be copied by the copy constructor and copy assignment operator.
But the parameters of the copy constructor and the copy assignment operator must be a Reference of the const type , and if not it will be an error ( you can try it yourself to see what the situation is ).
18. Assignment operators achieve copy assignment and move assignment two functions P478
If the parameter of the assignment operator is a call to a value , the initial taxiing parameter with the argument needs to call the copy constructor ( The argument is an lvalue ) or move the constructor ( the argument is the right value ). Then the equivalent copy assignment and move assignment can be implemented in the following way.
( Note : The following program = operator uses a custom swap because the standard library's swap requires class support = operator , but = operator we haven't defined yet )
19. Rvalue and Lvalue reference member functions P483
After the member function of the class, add & or && to qualify the member function to accept only lvalue or rvalue parameters.
You can also avoid operations such as using = Assignment for Rvalue objects.
C + + Primer Learning Summary 13th Chapter Copy Control