When I do this, the compilation works well
When you do this,
In this part, the expression displays an error as lvalue that cannot be modified. I wonder why compilation errors appear if I don't attach a reference because of the reference next to int.reference
int operator(int a);
The code above is int in the form of the return value of the function.
Because the function returns by default by value, the int-type value is returned to the place where this function was called. In other words,
p[i] returns an int-type number.
Therefore, the reason for the error in
p[i]=i; is that
int-type number is returned as a result of
number=i;, it became a code to substitute another number for a number, so the error "number is not lvalue" appeared.
int& operator(int a);
On the other hand, if you add a reference (&) after the int as above, it means that the value you want to return is a declared variable (whether it has a name or not), and the place where the return value is sent (the called place) is a nickname for the already declared variable (the value you want to return). The question code returns the ath of the class's member variable, arr. Therefore,
p[i]=i; is the nickname of
© 2022 pinfo. All rights reserved.