C ++ reference the declaration method and reference the declaration method

Source: Internet
Author: User

C ++ reference the declaration method and reference the declaration method

A reference is an alias of a variable. A reference operation is an action to the target.

The referenced declaration method:

    Type identifier & reference name = target variable name;

  For example:

1 int a; 2 int & ra = a; // Define reference ra, which is a reference of variable a, that is, alias

Note:

  • & Here, it is not an address operator but an identifier. The identifier declares a reference name;
  • The type identifier refers to the type of the target variable (that is, a of the above Code );
  • When declaring a reference, it must be initialized at the same time;
  • After the reference declaration is complete, the target variable name has two names: the original name and reference name of the target;
  • Declaring a reference is not a new variable. It only indicates that the reference name is an alias of the target variable name. Therefore, the system does not allocate a storage unit to the reference.

 

I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!

 

Example: Definitions and usage of references.

 

1 # include <iostream> 2 using namespace std; 3 4 int main () 5 {6 int a = 3; 7 int & B = a; 8 int & c =; // a variable can have multiple references 9 int & d = B; // reference the initialization reference 10 cout <B <endl; 11 cout <c <endl; 12 cout <d <endl; 13 return 0; 14} 15 16 // cainiao. Please criticize and advise, write code habits and specifications, and so on !!! Thank you !!!

 

 

I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!

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.