1

What will happen if I re refer a reference in c++ to some other object?

#include<iostrem> 
using namespace std; 
int main() {    
    int i,&j=i,k;    
    i=5;    
    cout<<i<<endl<<j<<endl;    
    j=k;    
    k=10;    
    cout<<j<<endl; 
}

I was expecting an error for this as I have read that we cant re refer a reference to another object but I got 21873, and as many times I am executing this program I am getting different results for last cout.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • You can't rebind a reference. `j=k` assigns `k` to whatever object `j` refers to. Since `k` is uninitialized, you have undefined behvaior and end up assigning garbage to `i`. – HolyBlackCat May 03 '19 at 17:04
  • oh yeah got it....Thank you very much for your valuable time. :-) – Rohan Arora May 03 '19 at 17:06

0 Answers0