When passing an object by value in C ++, the copy constructor is called to generate a copy of the object. Is that true in C?
Whether in C ++ or C #, there are two ways to pass a variable or object as a function parameter: pass by value or by reference.
The so-called pass by value means that the object copy is used inside the function body. In C ++, this copy is completed by calling the object copy constructor, the modification of the copy by the function does not affect the original object. For example
// C ++
Void fun1 (student SS)
{
... // Process and modify the SS-the actual processing is a copy of the input object
}
......
Student S7;
Fun1 (s7); // after this function is called, the status of object S7 does not change.
......
The so-called pass by reference refers to the address of the object that is passed to the function, so that the function changes the object and the object state changes. For example
// C ++
Void fun2 (student & SS)
{
... // Process and modify the SS
}
......
Student S8;
Fun2 (s8); // after this function is called, the status of the object S8 changes.
......
In C ++, you can use pointers and "&" referers to transmit references. In the above example, the reference symbol "&" can be used to replace it with a pointer to achieve the same effect. If we further think about it, we can find that when a pointer is used for reference transfer, replication also occurs, except that the value of the pointer (that is, the address of the object) is copied ), instead of copying the object pointed to by the pointer. This can be proved by the following example.
// C ++
Void fun3 (student * ss)
{
...... // Process and modify the object pointed to by the SS
Ss = NULL;
}
......
Student * S9;
Fun3 (S9); // after this function is called, the status of the object pointed to by S9 changes.
......
However, after fun3 (S9) is called, S9 is not null, which indicates that fun3 uses a copy of the pointer S9. Further, we can guess that the same story also happened when the "&" character was used to implement reference transfer. In fact, the reference in C ++ is only a restricted but safer pointer.
So what are the advantages of transferring by reference and passing by value?
The copy action is not required for transferring by reference, so the speed is fast, especially for large objects. When passing by value, the modification to the passed object is actually the modification to the object copy, without affecting the status of the original object.
You may think that if you use const reference transfer, you will get double benefits. You can say so, but do not go to the extreme.
Generally, large objects that cannot be changed are usedConstIt is appropriate to pass a reference to a function, but if it is a simple type or a custom small object, it can be passed directly with a value.
If the outside world must see the function's modification to the object, there is only one path ―― Pass by reference.
In C #, the situation has changed. Objects of the reference type in C # are passed by reference and can only be passed by reference. Value Type objects (or variables) are usually passed by value. If you want to pass a value type object by reference, you need to use the keyword ref or out. The only difference between ref and out is that when ref modifies a parameter, the input variable is initialized.
Because the class is a reference type, and all the objects of the reference type are passed by reference, there is no call to the copy function in this process. In this case, there is no need to copy the constructor.
Now you know why the constructor does not need to be copied or the value assignment operator is rarely called in C. You may ask if the assignment operator is rarely called, and the assignment operator must be called. What is the situation? That's because the similar body of the class-the structure struct.