Function declaration + Address-of operator

时间:2018-03-25 19:14:43

标签: c++

What exactly does function "func" return?

int a = 10;
int &func() {
    return a;
}

int main() {
    int b = func();
    std::cout << b; // prints 10
}

1 个答案:

答案 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.