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& 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