小编CYB*_*CYB的帖子

如何将自引用作为参数在C++中工作?

我感到困惑的代码之后,我想不通为什么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
推荐指数
1
解决办法
69
查看次数

标签 统计

c++ ×1