C + + pointer reference variable reference

Source: Internet
Author: User

Variable references:

The role of a reference is to give the variable an individual name, if there is a variable a, want to give it an individual name B, you can write: int a;//defines a is an integer variable. int &b=a;//Declaration B is a reference.

The above is a reference to B , that is, B is the alias of a . The "&" above is a reference declarator and does not represent an address. A and B occupy the same address space and memory unit. Declaration B is a reference to a, which can be understood as: Make the variable B have the address of variable A.

Reference as a function parameter:

The reason why C + + increases the reference mechanism is mainly to use it as a function parameter to extend the function of data transfer.

There are two cases of function parameter passing:

1. Make the variable famous as an argument and formal parameter. This is the value of the variable passed to the parameter, and the pass is one-way. If the value of the parameter changes during the execution of the function, it is not passed back to the argument. Because when a function is called, the formal parameter and the argument are not in the same storage unit.

2. Pass a pointer to the variable. A parameter is a pointer variable, an argument is the address of a variable, and a parameter (pointer variable) points to a real parametric cell when the function is called.

3. The reference provides a method for a "variable parameter" for a function call.

Note: When the &a is preceded by a type (such as int &a), it must be a declaration of the reference, or the address of the variable if there is no previous type character (such as Cout<<&a).

C + + pointer reference variable reference

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.