#include<iostream.h>
int main()
{
int m = 2;
int &x = m++;
cout<<m;
return 0;
}
this code should gives OUTPUT: 3
but this code gives error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int'.
int &x = m++;? - ocirocirm++is temporary object with lifetime restricted to this line. It cannot be assigned to reference - compiler message is quite clear. m++ keeps an old m value in temporary object, increments value of m, and returns temporary object with previous m value. - Alex F