我正在研究一个fraction类作为CS赋值,并编写了以下代码:
fraction fraction::add(fraction other) {
fraction temp;
/* manipulate temp */
return temp;
}
Run Code Online (Sandbox Code Playgroud)
这完美地工作,创建一个新fraction对象,然后将其返回到调用代码.
问题是,为什么这有效?temp当add方法返回并因此被销毁时,我的分数应该超出范围,但是它会被传递回调用代码而不会出现错误.
为什么返回具有局部范围的东西会导致它在超出范围后继续存在?