c++ reference

quote

definition:

type name & reference name = a variable name

int n = 4;
int &r = n;

A reference to a variable, equivalent to this variable, equivalent to an alias for the variable

Notice:

1. When defining a reference, be sure to initialize it to refer to a variable.

2. After initialization, it will always refer to the variable and will not refer to other variables

3. References can only refer to variables, not constants and expressions

application

swap two variables

void swap( int & a, int & b)
{
int tmp;
tmp = a;a = b; b = tmp;
}

as the return value of the function

int n = 4;
int & SetValue(){return n;}

often quoted

When defining a reference, add the const keyword in front of it

Can't modify the content of its citation through constant citation (but can do it in other ways)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325991985&siteId=291194637