This article mainly shares with you the citation summary in C++. Friends who need it can take a look.
Quote summary
(1) In the use of references, it is meaningless to simply give an alias to a variable. The purpose of references is mainly used to solve the problem of transferring large pieces of data or objects in function parameter transfer. Issues of unsatisfactory efficiency and space.
(2) Using reference to pass function parameters can ensure that no copies are generated during parameter transfer, improving the efficiency of transfer, and through the use of const, the security of reference transfer is ensured.
(3) The difference between a reference and a pointer is that after a pointer points to an object through a pointer variable, it operates indirectly on the variable it points to. The use of pointers in the program makes the program less readable; the reference itself is an alias of the target variable, and the operation on the reference is the operation on the target variable.
(4) Timing of using references. It is recommended to use references for the stream operators << and >>, the return value of the assignment operator =, the parameters of the copy constructor, the parameters of the assignment operator =, and other situations.
(1) Passing a reference to a function has the same effect as passing a pointer. At this time, the formal parameter of the called function becomes an alias of the actual parameter variable or object in the original calling function. Therefore, the operation on the formal parameter variable in the called function is to operate on its corresponding target object (in the main calling function). call function) operation.
(2) Using reference to transfer function parameters does not produce a copy of the actual parameters in the memory. It directly operates on the actual parameters; while using general variables to transfer function parameters, when a function call occurs, A storage unit needs to be allocated to the formal parameter. The formal parameter variable is a copy of the actual parameter variable; if an object is passed, the copy constructor will also be called. Therefore, when the data passed by parameters is large, using references is more efficient and takes up less space than using general variables to pass parameters.
(3) Although using pointers as function parameters can also achieve the same effect as using references, storage units must also be allocated to formal parameters in the called function, and "* pointer variable names need to be reused. " is performed in the form of ", which is prone to errors and makes the program less readable; on the other hand, at the call point of the main calling function, the address of the variable must be used as the actual parameter. Quotes are easier to use and clearer.
If you want to use references to improve program efficiency and protect the data passed to the function from being changed in the function, you should use constant references
Related recommendations:
The above is the detailed content of Summary of references in c++. For more information, please follow other related articles on the PHP Chinese website!