#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'.
Post increment operator returns a temporary object with the value of m before increment.
Non const lvalue references can't be bound to temporary objects. Thus, the compiler rightfully complains.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With