R1 is a reference bound to 1

Mar 17, 2016 at 9:32am
ri is a reference bound to i so how come it prints 10, 10 ?
1
2
3
  int i, &ri = i;
i = 5; ri = 10;
std::cout << i << " " << ri << std::endl;


Mar 17, 2016 at 9:36am
ri refers to i so when you do ri = 10; you are setting the value of i to 10.
Last edited on Mar 17, 2016 at 9:37am
Mar 17, 2016 at 9:50am
It's ri = 10 and nowhere it says i = ri
Mar 17, 2016 at 9:56am
See that & on the first line? That means ri is a reference to i. You can think of ri as an alternative name for i. When you use ri in the code you are actually using i.
Topic archived. No new replies allowed.