I fail to understand why the following program wrong:
int& getID(){
static int r = 0;
return r++;
}
main:
int main(){
int a = getID();
std::cout << "a=" << a << std::endl;
return 0;
}
Why returning a static variable as described creates problems and not returning the wanted value?
error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’r++returnsint, notint &. Tryr++; return r;instead.r++doesn't "return" anything. It evaluates to a prvalue.