What does “int& foo()” mean in C++?

前端 未结 9 1901
不思量自难忘°
不思量自难忘° 2021-01-30 11:59

While reading this explanation on lvalues and rvalues, these lines of code stuck out to me:

int& foo();
foo() = 42; // OK, foo() is an lvalue
9条回答
  •  轮回少年
    2021-01-30 12:49

    The function you have, foo(), is a function that returns a reference to an integer.

    So let's say originally foo returned 5, and later on, in your main function, you say foo() = 10;, then prints out foo, it will print 10 instead of 5.

    I hope that makes sense :)

    I'm new to programming as well. It's interesting to see questions like this that makes you think! :)

提交回复
热议问题