2.5 reference

Source: Internet
Author: User

1. The reference is an alias. The reference is just another name of the object.

2.

Code:
  1. Int ivals = 1024;
  2. Int & ref = ival;

3. Define multiple references

Code:
  1. Int ival = 1024; iival = 1025;
  2. Int & ref1 = ival, & ref2 = iival;

4. A const reference is a reference to a const object.

Code:
  1. Const int ival = 1024;
  2. Const Int & ref = ival;
  3. Int & ref2 = ival; // Error

5.

Code:
  1. // Compilation failed
  2. Float ivalue = 3.14;
  3. Int & Reff = ivalue;
  4. // Compilation successful
  5. Const float ivalue = 3.14;
  6. Const Int & Reff = ivalue;
  7. Different types of conditions.

================================= 4.18 day review updates ======================== ======================================

Code:
  1. // Compilation successful
  2. Const float ivalue = 3.14;
  3. Const Int & Reff = ivalue;

Compiler:

Const int temp = ivalue;

Const Int & Reff = temp;

Ivalue is not modified

 

A const reference can be bound to a different but associated type object or to the right value.

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.