my code:
#include <iostream>
using namespace std;
int a = 100;
int func();
int main()
{
int c = 10;
int & d = c;
// int & b = func();
int & b = a;
cout << b << endl;
return 0;
}
int func()
{
return a;
}
if I open the commment line. My g++ compiler will prompt error cannot bind non-const lvalue referrence of type int& to an rvalue of type int
. This is so strange. This statement is so similar to its next line int & b = a
. Why its next line can work well and do not prompt the same error.
And another strange is after I modify my func definition which I modify its return type form int
to int &
the g++ will not prompt the error.
I usually use java and I just learn c++ last week. I need some experts help.