我感到困惑的代码之后,我想不通为什么Test t作为参数calc,并return t会打电话Test(Test &t)?任何人都可以帮我说清楚吗?非常感谢!
#include <iostream>
using namespace std;
class Test {
public:
Test(int na, int nb) {
a = na;
b = nb;
}
Test(Test &t) {
a = t.a + 1;
b = t.b + 1;
}
int getValue() {
return a + b;
}
Test calc(Test t) {
return t;
}
private:
int a;
int b;
};
int main() {
Test t(1, 1);
cout << t.calc(t).getValue() << endl;
}
Run Code Online (Sandbox Code Playgroud) c++ ×1