In C + +, the function's pass-through method has value passing and address passing. The pass-through address has pointers and reference methods.
In the function arguments, the reason for the address is:
1. Enable the modulated function to change the data object in the keynote function;
2. The address can reduce the data copy, improve the speed of program operation.
So, when to use value delivery, when to use address delivery (pointers and references)?
Here are some of the things you might encounter when writing code
The data object is not altered when the function uses the Parameter object
- Suppose that the data object to pass in the function is small, for example, a built-in data type or a small-scale structure, then use the value to pass;
- Assuming the data object is an array, use the pointer (which is also your only option). The pointer is added to the const modifier;
- Suppose the data object is a large-scale struct, using a const pointer or a const reference;
- Assume that the data object is a class object, using a const reference. This is also the standard practice of passing class objects in C + +;
The data object is changed when the function uses the Parameter object
- The data object is the built-in data type, using the pointer (func (&VAR) to indicate that Var is to be changed in the function, which is better readable than the invocation form func (VAR) using the reference;
- The data object is an array, and the pointer is the only choice;
- A data object is a struct, using pointers or references;
- The data object is a class object, using References;
Of course, these are just ordinary situations. Special cases are discussed additionally.
Parameter Pass (reference, pointer, value pass) c++11