C + + Reference

Source: Internet
Author: User

C++A reference reference variable is an alias, that is, it is another name for an existing variable. Once the reference is initialized to a variable, you can use that reference name or variable name to point to the variable. C++reference vs pointer references are easily confused with pointers, and there are three main differences between them: there is no null reference. References must be connected to a piece of legitimate memory. Once a reference is initialized to an object, it cannot be pointed to another object. The pointer can point to another object at any time. The reference must be initialized at the time of creation. Pointers can be initialized at any time. C++Create a reference in the Imagine variable name is a tag attached to a variable in the memory location, you can refer to the reference as the second label in the memory location of the variable attached. Therefore, you can access the contents of a variable through the original variable name or reference. For example:inti = -We can declare reference variables for I, as follows:int&amp; R =I; In these statements,& read as a reference. Therefore, the first declaration can be read as a"r is an integer reference initialized to I", a second declaration can be read as"S is a double-type reference that is initialized to D"。 The following example uses theintAndDoubleReferences: #include<iostream>using namespacestd;intMain () {//declaring a simple variable   inti; DoubleD; //declaring reference variables   int& r =i; Double& s =D; I=5; cout<<"Value of I:"<< I <<Endl; cout<<"Value of I reference:"<< R <<Endl; D=11.7; cout<<"Value of D:"<< D <<Endl; cout<<"Value of D reference:"<< s <<Endl; return 0;} When the above code is compiled and executed, it produces the following result: Value of I:5Value of I reference:5Value of D:11.7Value of D reference:11.7

C + + 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.