[001] Initial reference

Source: Internet
Author: User

  A reference (reference) is the equivalent of another name for the object. The type refers to a different type. The reference identifier begins with "&".

1 int val =; 2 int &reference = val;     // Refval point to Ival 3 int &mistake;              // Error: Reference must be initialized

From the above example, you can see that the initial value is copied to the newly created object when the variable is initialized. When you define a reference, the program binds the reference to its initial value. Once the initialization of the reference is complete, the binding relationship will persist, so the reference must be initialized .

Note that a reference is an alias , and it is not an object.

1 int Ten ; 2 int &refi = i; 3 1;             // I was assigned to the 1 4 int a = refi;        // equivalent to make a = i

The initial value of the reference must be an object, and the referenced type must be strictly consistent with its object, so:

1 int &ref0;        // Error 2 Double 1.12 ; 3 int &refa = A;      // Error

[001] Initial 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.