Can we reassign the reference in C++?

ri = j; // >>> Is this not reassigning the reference? <<<

No, ri is still a reference to i – you can prove this by printing &ri and &i and seeing they’re the same address.

What you did is modify i through the reference ri. Print i after, and you’ll see this.

Also, for comparison, if you create a const int &cri = i; it won’t let you assign to that.

Leave a Comment