What exactly does function "func" return?
int a = 10;
int &func() {
return a;
}
int main() {
int b = func();
std::cout << b; // prints 10
}
答案 0 :(得分:1)
The &
means a reference here, not the address operator. So func()
returns a reference to an int. When you call it, it returns a reference to the variable a
, and when you assign it to the int b
, it copies the value in a
to b
.