I am baffled how come the following program works fine. I am returning a reference to local variable from a function and a reference is assigned a value more than once. I would expect compiler to throw an error for reference assignment.
#include <iostream>
using namespace std;
int& getNum()
{
int myNum = 89;
return myNum;
}
int& getAnotherNum()
{
int myNum = 1000;
return myNum;
}
int main()
{
int& Value1 = getAnotherNum();
cout << "Value1 value is: " << Value1 << endl;
Value1 = getNum();
cout << "Value1 value is: " << Value1 << endl;
return 0;
}