Reasons for using constant reference as much as possible

Source: Internet
Author: User

since C ++ is a course in this semester, although I used to study Java, C ++ is still useful due to the pressure of the course exams, so I took a look. At the beginning, I was interested in C ++ references, because in Java, I never thought about references, because for objects, java uses the form of passing by reference, and I don't know if it is because of my getting started Java programming ideas, I already have C ++ or C experience by default, so for reference, basically, it's just a noun. But here in C ++, I can see the full picture of the reference (Because Java originally originated from C ++ ^-^ ). References in C ++ are divided into constant references and constant references. The difference between the two lies in that the former has a const modifier, but the meaning is very different. A non-constant reference can be used to modify the value of the passed object. For example, if we exchange the values of two objects, we need to transmit the references of the two objects, only copying objects is passed in. The original objects are not exchanged, and constant references are not allowed to modify parameters. But is this behavior really good? Directly modifying the parameter value can easily cause problems, and it is difficult for us to find this error in a large Program , even if the tracing object finds this error, who knows which of the above errors will happen? Therefore, if we want to modify the object and make sure that there will be no more use of the object, we can use a very large number of references, but you must pay attention to this, we want to read this kind of mentality in the Code . If you see a very large number of references, you can think that this parameter has been modified, you must be careful when calling a function to ensure that it works as expected.

Constant references cannot be modified. This strict limitation makes sense and makes it safer to use it than to use it with a very large amount of reference. More importantly, on the surface, it seems that constant references are more demanding than constant references, but actually they are larger than the former! For example, I have the declaration of such a function: void F (Int & T); but in the test, I call: F (2); this will cause problems, this is because I passed in a very mass reference, and it should be an object, but I passed in a constant during the call. Therefore, a very large number of references will be ineffective and an error will be reported. But if it is a constant reference, it can cope with this situation! This is because constant reference has a special feature that enables the compiler to create a temporary object to handle this situation. Let's take a detailed look at this process: if I change the Declaration to void F (const Int & T); then when I call function f () like this, the compiler will create a temporary object, the object will be initialized as constant 2, and t will point to this temporary object, so we will still pass the object rather than a constant. Therefore, constant reference can solve the problem of parameter Type Mismatch through this mechanism, and prevent the parameter from being modified to make our data more secure. Therefore, we should try to use constant references as much as possible, because constant references can solve the problems that can be solved (through temporary objects)-unless you really want to modify your parameters, in addition, it can also provide security protection. Why?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.