[001] First-recognized reference, 001 first-recognized reference

Source: Internet
Author: User

[001] First-recognized reference, 001 first-recognized reference

Reference is equivalent to another name for the object. This type references another type. The referenced identifier starts.

1 int val = 180; 2 int & reference = val; // refVal points to ival3 int & mistake; // error: the reference must be initialized

The above example shows that the initial value is usuallyCopyTo the new object. When defining a reference, the program refers to the reference and its initial value.BindTogether. Once the initialization of the reference is complete, the binding link will always exist. Therefore, the reference mustInitialization.

Note,AliasIt is not an object.

1 int I = 10; 2 int & refi = I; 3 refi = 1; // assign 1 to i4 int a = refi; // equivalent to making a = I

 

The initial value must be an object, and the referenced type must be exactly the same as the object. Therefore:

1 int & ref = 0; // Error 2 double a = 1.12; 3 int & refa = a; // Error

 

Related Article

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.