-1

What exactly does function "func" return?

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

int main() {
    int b = func();
    std::cout << b; // prints 10
}
Borgleader
  • 15,826
  • 5
  • 46
  • 62
Etwus
  • 549
  • 5
  • 15

1 Answers1

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.

eesiraed
  • 4,626
  • 4
  • 16
  • 34