【发布时间】:2016-09-24 04:44:42
【问题描述】:
我是编程新手,我们刚刚开始学习“课程”。我将向您展示我在互联网上找到的示例代码。我的问题是 - 是 "add" 和 "res" 构造函数,构造函数如何返回值? “X res 和 X add” 不是 int 类型的方法,它仍然返回一个值(res 也没有变量),所以我真的很困惑.. 我见过在 stackoverflow 的一些帖子中,构造函数无法返回值,但是 "X res and X add" 是什么?
#include <iostream>
using namespace std;
class X {
int a;
int b;
public:
X (int a=7, int b=6) {
this->a = a;
this->b = b;
}
void print() {
cout << a << b;
}
X add() {
X res(a+b, a-b);
return res;
}
};
int main() {
X x;
x.add().print();
return 0;
}
【问题讨论】:
标签: c++ class object constructor return-value