<an*******@gmail.comwrote in message
news:11*********************@j4g2000prf.googlegrou ps.com...
hello m newbie at here now i have a problem with the "Reference" in "C+
+". i am following the Book("C++ Primer,Fourth Edition")
there is The Question
#include<iostream>
int main()
{
int i, &ri = i;
i = 5; ri =10;
std::cout << i << " " << ri << std::endl;
}
and the compiler return the answer 10 10
can there anyone who can tell me about it
how compiler take the value and return it
Andry
To format your program a little better (IMO)
#include<iostream>
int main()
{
int i;
// i is now an integer
int &ri = i;
// ri is a reference to an integer, in this case i. Whenever we deal
with ri
// it is just as if we are dealing with the orignal variable i
i = 5;
// That assigned the value of i to 5
ri =10;
// That assigned the value of i (which ri is a reference to) to 10.
std::cout << i << " " << ri << std::endl;
// i and ri both refer to the same variable i, which has 10 in it.
}