Differences Between Reference transfer and pointer transfer in C ++

Source: Internet
Author: User

. During the value transfer process, parameters in the form of the called function are processed as local variables of the called function, that is, the memory space is opened in the stack to store the values of the real parameters put in by the main function, thus. The feature of value transfer is that any operation of the form parameter of the called function is performed as a local variable, without affecting the value of the real parameter variable of the main function. (The address value of the real parameter pointer will not change here)

. Because of this, any operation performed by the called function on the form parameter affects the real parameter variables in the main function.

, The reference is more than the pointer type check

. Because

.

 

.

. A reference must always point to some objects. Therefore, if you use a variable and direct it to an object, but the variable may not point to any object at some time, you should declare the variable as a pointer, in this way, you can assign a null value to the variable. On the contrary, if the variable must point to an object, for example, your design does not allow the variable to be empty, then you can declare the variable as a reference. "But please wait." You may doubt, "What are the consequences of such code? "

{

}//

V [5] = 10 ;//

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.