An incomplete summary of a C + + reference

Source: Internet
Author: User
Tags constant

1. Reference base

A reference is a different name for an object, usually in the form of "&d" to define the reference type, where D is the declared variable name:

int ival = 1024;
int &refval = ival;
References must be initialized, references are not objects, just another name for an existing object. After a reference is defined, all operations on it are performed on the object to which it is bound:

Refval = 2;
int II = Refval;
The reference itself is not an object, so the reference cannot be defined

This article address: http://wuyudong.com/2016/11/10/2971.html, reprint please indicate the source.

2, reference to the pointer

Because the reference itself is not an object, naturally there is no pointer to the reference, but the pointer is an object, all references that can define the pointer:

int i = 1;
int *p;
int *&r = p; R is a reference to the pointer p
r = &i; R references a pointer, which is P pointing i
*r = 0; Solution Reference R Get I
3, the const reference

You can bind a reference to a const object as a reference to a constant, and a reference to a constant cannot be used to modify the object to which it is bound:

const int CI = 1024;
const int &R1 = CI;
R1 = 42; ERROR:R1 is a reference to a constant
int &r2 = CI; Error: Attempting to have a very reference to a constant object
An arbitrary expression is allowed as an initial value when initializing a constant reference, provided that the result of the expression can be converted to a reference type, especially for a constant reference to a bound object, a literal, or even a general expression:

int i = 42;
const int &R1 = i;
const int &R2 = 42;
const int &R3 = r1 * 2;
int &R4 = r1 * 2; ERROR:R4 is a normal, extraordinary amount of reference
4. function Pass Reference parameter

Allows a function to change the value of one or more arguments by reference parameters

void Reset (int &i)
{
i = 0;
}
int j = 23;
Reset (j);
cout<< "J =" <<j<<endl; j = 0
Tip 1: Use references to avoid copying

Copying large class-type objects or container objects is inefficient, and even some class types (including IO types) do not support copying at all, and can be accessed by reference parameters, for example:

BOOL Isshorter (const string &S1, const string &s2)
{
String object may be very long, select reference
Return S1.size () < S2.size ();
}
Tip 2: Use reference parameters to return additional information

If there are times when a function needs to return more than one value, the reference parameter provides the possibility for that requirement.

For example: Implement a function that returns the first occurrence of a specified character in a string object and returns the total number of occurrences of that character

String::size_type Find_char (const string &s, char C, String::size_type &occurs)
{
Auto ret = S.size ();
Occurs = 0;
For (Decltype (ret) i = 0; I!= s.size (); i++) {
if (s[i] = = c) {
if (ret = = S.size ()) {
ret = i;
}
occurs++;
}
}
return ret;
}
Auto index = Find_char (s, c, occurs)
5, array reference parameters

C + + allows a variable to be defined as a reference to an array, so that the parameter can be a reference to the arrays, at which point the reference parameter is bound to the corresponding argument, which is bound to the array:

void print (int (&array) [10])
{
for (auto Elem:array)
cout<<elem<<endl;
}
int k[10] = {0,1,2,3,4,5,6,7,8,9};
Print (k);
6, function return reference

If the function returns a reference, the reference is only an alias for the object it refers to, for example:

Const string &shorterstring (const string &S1, const string &s2)
{
Return s1.size () <= s2.size ()? S1:S2;
}
Where the formal parameter and the return result are references to a const string so that the string object is not actually copied whether the call function or the result is returned.

Calling a function that returns a reference gets the left value, and the other return type gets the right value, so that we can assign a value to the result of the function that the return type is a very important reference:

Char &get_val (string &s, String::size_type i)
{
return s[i];
}

Int main ()
{
    string s ("a bug!");
    cout<<s<<endl; /output: A bug
    get_val (s, 0) = ' a ';
& nbsp;   cout<<s<<endl; Output: A bug
    System ("PAUSE");
    return 0;
}
This article summarizes the various usages that are referenced in C + +, and then supplements it later.

Related Article

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.