Questions tagged «return-by-reference»

9
C ++中的“ int&foo()”是什么意思?
在阅读有关左值和右值的解释时,这些代码行向我伸出来: int& foo(); foo() = 42; // OK, foo() is an lvalue 我在g ++中尝试过,但是编译器说“对foo()的未定义引用”。如果我加 int foo() { return 2; } int main() { int& foo(); foo() = 42; } 它可以很好地编译,但是运行它会产生分段错误。只是线 int& foo(); 本身既可以编译又可以毫无问题地运行。 此代码是什么意思?如何为函数调用分配值,为什么它不是右值?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.