Common uses of references in C + +

Source: Internet
Author: User

  Reference type is a powerful existence in C + +, it avoids the risk of pointers to some extent, and makes the transfer value and communication between function parameters extremely flexible.

  1. The concept of references

  In C + +, you can define a reference as follows:

1 int N; 2 int &r = n;

  After this definition, R is a reference to the INT variable n. It can be understood that by giving the variable n an "alias" called R, the R is bound together with N. Modifying the value of R is actually equivalent to modifying the value of N, and using R is actually equivalent to using N.

 It is important to note that a reference type must be initialized when it is defined (as the code above initializes it to N)-it is natural to imagine that if we say an alias to an item, but do not specify what is to be given an alias, then the "Alias" thing is meaningless.

  The following sample code is derived from the new standard C + + programming Tutorial:

1#include <iostream>2 using namespacestd;3 intMain ()4 {5     intn =4;6     int&r = n;//R refers to the n7R =4;//modified R is a modified n8cout << R << Endl;//Output 49cout << n << Endl;//Output 4Tenn =5;//modified n is a modified R Onecout << R << Endl;//Output 5 A     int&AMP;R2 = R;//R2 and R refer to the same variable n -cout << R2 << Endl;//Output 5 -     return 0; the}

  2. Reference as the return value of a function

  The return value of a function can also be a reference. Using a reference as the return value of a function can play a huge role in operator overloading.

1#include <iostream>2 using namespacestd;3 intn =4;//n is a global variable4 int&SetValue () {5     returnN//returns a reference to n6 }7 intMain ()8 {9SetValue () = +;//The return value is a referenced function call expression that can be used as an lvalue, equivalent to modifying the value of nTencout << n << Endl;//Output One     int&r = SetValue ();//R indirectly refers to n through the function SetValue () Acout << R << Endl;//Output -     return 0; -}

3. Referencing the formal parameter as a function

  If a function's formal parameter is defined as a reference type, then when the function is called, a reference to the argument is passed in to the function. If the value of the passed parameter is changed in the function body, the value of the argument outside the function will also change.

1#include <iostream>2 usingNamspace std;3 voidSwap (int&a,int&b) {4     inttmp;5TMP =A;6A =b;7b =tmp;8cout <<"In swap:a ="<< a <<"B ="<< b <<Endl; 9 }Ten intMain () One { A     intA =4, B =5; - Swap (A, b); -cout <<"After swaping:a ="<< a <<"B ="<< b <<Endl; the     return 0;  -}

  The output is:

5 4  = 5 B = 4

4. Frequently cited

  If you add the Const keyword when you define a reference, the reference becomes a constant reference. Such as:

1 int N; 2 Const int &r = n;

  The difference between a regular reference and a normal reference is that it is not possible to modify the contents of the reference by a common reference. Note: Content that is not commonly referenced cannot be modified, but it cannot be modified by a regular reference. such as:

1 int  - ; 2 Const int &r = N; 3 ;    // compilation error, unable to modify the contents of its reference through a common reference 4 ;   // no problem, the value of n is changed to

  The const T & and T & are different types. A reference to a T & type or a variable of type T can be used to initialize a const T & type Reference. A const t type constant variable and a const T & type reference cannot be used to initialize a reference to a T & type unless coercion is cast. For example, the following program segment:

1 intMain ()2 {3     Const CharCC ='a';4     CharC;5     Const Char&AMP;RC1 = CC;//OK,CC can neither be modified nor modified by RC16     Const Char&AMP;RC2 = C;//Ok,c can be modified, but cannot be modified by RC27     Char&r = CC;//Error,const char type cannot be used to initialize char type8     Char&AMP;R2 = (Char&) cc;//OK, cast const char to char type reference9cout << R2 << Endl;//the output a,r2 points to ccTenr2 ='b'; Onecout << cc << Endl;//output A, cannot modify CC's value through R2 Acout << R2 << Endl;//The output b,r2 points to character B, which refers to the position of character B in memory -     return 0; -}

  5. Summary

  In C + +, declaring a variable to be a reference, you must initialize it, and the initialization can be either a variable name or a specific value (e.g. int &a = 2; char &b = ' B '). A reference can be used as the return value of a function, at which point it can be assigned a value as an lvalue. A reference can be a parameter to a function, and when a function is called, it is actually a reference to the argument, not a copy, that is passed to the function. Therefore, if the value of the passed parameter is modified in the function, the value of the referenced variable will also change. You can use the Const keyword to define a constant reference, and you cannot modify the value of the referenced variable by using a constant reference.

Common uses of references in C + +

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.