Reference in lhs and rhs difference in C ++

I am learning C ++ and I found that when the link is on the right hand side there can be two cases. Suppose I have a method:

int& GetMe(int& i) { return i; }

      

And I have:

(1) int j; GetMe(j) = GetMe(i);

and

(2) int& k = GetMe(i);

Consequences (1) and (2) are different. In (1), the semantics is to copy the value i

to j

. The addresses i

and j

remain unchanged. The change i

does not affect at all j

. This is actually the case when you overload the index operator [] and use the index operator for assignment. In (2), the semantics is to create a referent i

like k

. k

has the same address as i

, and the change i

affects k

.

So why do we have differences? I think in C ++ the referenced address is defined once and only once. Once the referenced address is determined, it can never be changed later. In (1), the reference j

is defined earlier, so the action must copy the value from i

to j

. In (2), the link is k

declared and initialized, so it is executed using the link i

. Thus, the action is the initialization of links.

I have not found material explicitly speaking above, so I want confirmation. Anyone who knows the C ++ reference well should help me or point me to clear stuff. Thank you so much for the advanced one.

+2


source to share


3 answers


What you are missing here is that the type of the variable is different and it all matters. In the first example you have int j

, and in the second you have int &k

.

References in function prototypes exist for different reasons, they look the same because they are almost the same, but they are used differently because they only exist when the method is executed.

Actually, your don code matches exactly

int j;
int & j1 = j;
int & i1 = i;
j1 = i1;

      



against

int & i1 = i;
int & k = i1;

      

It is easy to see that in the first case, the two references actually refer to different variables, that is, to different parts of memory, but in the second case they all refer to the same variable. Hence the difference.

+5


source


In case 1, GetMe returns a reference to j and then assigns the value to j via that reference. In case 2 GetMe returns a reference to i and then you assign that reference to another reference k meaning k refers to i.



References do not change in C ++ (this means that you cannot change the reference to a link, although you can change the value of the referent). Links are also transparently replaced with whatever they refer to when used on the right side of an expression. Immutability of references means there is no ambiguity in 1 - you should intend to assign the return value to GetMe, not change the reference. There is no ambiguity in 2 because you are assigning a reference to a reference (you cannot assign an int to a reference to an int).

+3


source


The difference is that in the second example you are defining a link and in the first example you are assigning an existing link.

0


source







All Articles