Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What matters should be paid attention to in the citation in C++?

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what matters need to be paid attention to in the citation of C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn what matters need to be paid attention to in the citation of C++.

Citation summary

The main results are as follows: (1) in the use of reference, it is meaningless to simply give an individual name to a variable, and the purpose of reference is mainly used to solve the problems of transmission efficiency and spatial dissatisfaction of large chunks of data or objects in the transfer of function parameters.

(2) using the parameters of the reference transfer function can ensure that there is no copy in the parameter transfer, improve the transfer efficiency, and ensure the security of reference transfer through the use of const.

(3) the difference between a reference and a pointer is that after pointing to an object through a pointer variable, the pointer operates indirectly on the variable it points to. When pointers are used in the program, the readability of the program is poor; and the reference itself is the alias of the target variable, and the operation on the reference is the operation on the target variable.

(4) the timing of using citation. References are recommended for the stream operator, the return value of the assignment operator =, the parameters of the copy constructor, the parameters of the assignment operator =, and others.

(1) passing a reference to a function has the same effect as passing a pointer. At this time, the formal parameter of the modulated function becomes an alias of the argument variable or object in the original tone function, so the operation on the parameter variable in the modulated function is the operation on its corresponding target object (in the tone function).

(2) using the parameters referencing the transfer function, there is no copy of the argument in memory, it operates directly on the argument; while using the general variable to transfer the parameter, when the function call occurs, you need to assign a storage unit to the parameter. The formal parameter variable is a copy of the argument variable; if the object is passed, the copy constructor will also be called. Therefore, when the data passed by the parameter is large, the efficiency and space occupied by using reference is better than that with general variable.

(3) although using the pointer as the parameter of the function can also achieve the effect of using reference, it is also necessary to assign a storage unit to the parameter in the adjusted function, and the operation needs to be repeated in the form of "* pointer variable name". This is easy to produce errors and the readability of the program is poor; on the other hand, at the call point of the main tone function, the address of the variable must be used as the argument. References are easier to use and clearer.

If you want to use references to improve the efficiency of the program while protecting the data passed to the function from being changed in the function, you should use frequent references

Thank you for your reading. The above is the content of "what matters need to be paid attention to in the citation of C++". After the study of this article, I believe you have a deeper understanding of what matters need to be paid attention to in the citation of C++. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report