1. l value required

Hi,

I had some of the issues referred to "lvalue required" in c++, but i could not find for my specific case.
I was trying to write a small code using "Type reference" concept.

Below is my simple code:

Code:
```#include<iostream.h>
#include<conio.h>

int main()

{
clrscr();
int y1=5,y3=6;
int & y2 = y1;

cout << "y2 is "<< y2 <<endl<<" address of y2: "<< &y2 <<"\n";

&y2 = &y3;

cout << "y2 is "<< y2 <<endl<<" address of y2: "<< &y2 <<"\n";

getch();
return 0;
}

```

Now i get the error saying "lvaue required".

anil

2. What did you intend by writing this line?
Code:
`&y2 = &y3;`

3. >&y2 = &y3;
This is wrong. The address isn't a lvalue.

Also, you can not ..in any way, unset a reference..if that is what you were trying .

4. Well.. i was trying to point the value of y3 to y2.

(That is how it is shown in text book sample code)

in the initial first statements, i have assigned the value of y1 to y2
int & y2 = y1;

and then i want to overwrite the values y1 by y3, which is assigned to y2
&y2 = &y3;

5. Originally Posted by anil_
Well.. i was trying to point the value of y3 to y2.

(That is how it is shown in text book sample code)

in the initial first statements, i have assigned the value of y1 to y2
int & y2 = y1;

and then i want to overwrite the values y1 by y3, which is assigned to y2
&y2 = &y3;
Overwriting the value and trying to change the address, are totally different .
You can NOT make y2 point to y3.
(You could, if y2 was a pointer instead of a reference )

6. Originally Posted by anil_
in the initial first statements, i have assigned the value of y1 to y2
int & y2 = y1;
No, you did not assign the value of y1 to y2. Rather, you made y2 an alias for y1.

Originally Posted by anil_
and then i want to overwrite the values y1 by y3, which is assigned to y2
&y2 = &y3;
Since y2 is an alias of y1, you should write:
Code:
`y2 = y3;`
Originally Posted by anil_
i was trying to point the value of y3 to y2.
It sounds more like you were trying to set the value of y2 to the value of y3.

7. ok i got the point...

8. Originally Posted by anil_
But now, how do i change the code so that the address of y2 and y3 are same.
As manasij7479 has stated, you cannot rebind the reference. y2 is an alias for y1; it cannot become an alias for y3, in this context.

9. y2 = y3;

But for the above code, the address of y3 is different from y2.

I need to change the address of y2 to be same as y3.
what should i do then..?

anil

10. Originally Posted by laserlight
As manasij7479 has stated, you cannot rebind the reference. y2 is an alias for y1; it cannot become an alias for y3, in this context.
i agree with manasij7479 that it is not possible trying to play with 3 variables point to 2 addresses at the same time. It should be only two on two.

But in my case, y2 should be an alias to y1, initially

and then y2 should become an alias to y3, finally

anil

11. Originally Posted by anil_
i agree with manasij7479 that it is not possible trying to play with 3 variables point to 2 addresses at the same time. It should be only two on two.

But in my case, y2 should be an alias to y1, initially

and then y2 should become an alias to y3, finally

anil
You misunderstood what I meant.
Once you 'set' a reference as an alias for a variable, you can not change it to 'point' to another variable .

12. ok, i think the text book is wrong.

In the book, he talks of changing the reference as an alias for another variable.

Any how i thank you all for this nice conversation we had. it was such a nice KT among each other.

Once again thank you very very much.

anil

13. But in my case, y2 should be an alias to y1, initially

and then y2 should become an alias to y3, finally
In the book, he talks of changing the reference as an alias for another variable.
Just to be completely clear, that is strictly prevented by the language.

Could you tell us which book tries to incorrectly suggest that it can be done please?

14. Originally Posted by iMalc
Could you tell us which book tries to incorrectly suggest that it can be done please?
"Object Oriented Programming with C++ : M.P.Bhave and S.A. Patekar"

what confuses me is, there is an output text window with all the results. I have mailed to author asking for further clarifications.