考虑以下 C++ 代码:
// friend functions
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);}
friend CRectangle duplicate (CRectangle);
};
void CRectangle::set_values (int a, int b) {
width = a;
height = b;
}
CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres; // Defined without using new keyword. This means scope of this variable of in this function, right ?
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
int main () {
CRectangle rect, rectb;
rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
}
变量“CRectangle rectres”在函数“CRectangle duplicate”中定义。
这是否意味着变量“CRectangle rectres”的范围仅限于函数?(因为它是在没有使用 new 关键字的情况下定义的)
如果上述问题的答案是肯定的,那么如何返回(因为它是局部变量)?