【发布时间】:2019-06-30 11:33:08
【问题描述】:
试图通过 get/set 方法修改类中的对象。我无法理解如何仅使用 get/set 方法更改值。
预期输出:“输出:89”。
实际输出:“输出:0”
#include<iostream>
using namespace std;
class TestClass{
public:
int getValue() const{
return _value;
}
void setValue(int value) {
_value = value;
}
private:
int _value;
};
class A{
public:
TestClass getTestClass() const{
return _testClass;
}
void setTestClass(TestClass testClass) {
_testClass = testClass;
}
private:
TestClass _testClass;
};
int main()
{
A a;
a.getTestClass().setValue(89);
cout<<"Output :"<<a.getTestClass().getValue();
}
【问题讨论】:
-
在 main() 中尝试
a.getTestClass();后跟a.setValue(89);