这是一个说明我的问题的代码片段:
class A {...};
const A& foo1() {...}
const A& foo2() {...}
void foo3(int score) {
if (score > 5)
const A &reward = foo1();
else
const A &reward = foo2();
...
// The 'reward' object is undefined here as it's scope ends within the respective if and else blocks.
}
如何在 if else 块之后访问reward
对象?foo3()
这是避免代码重复所必需的。
提前致谢 !