此題主要考察this指針,以及引用作左值
#include <iostream>
using namespace std;
class A
{
public:
int val;
// 在此處補充你的代碼
A(int x = 123 )
{
val = x;
}
//************這部分我想了好久也沒想出來
A& GetObj()
{
return *this;
}
//**************很精髓 結論:傳回左值盡量是 this指針指向的對象
};
int main()
{
int m,n;
A a;
cout << a.val << endl;//輸出 1,2,3
while(cin >> m >> n)
{
a.GetObj() = m; //m被強制轉換成另一個a類型
cout << a.val << endl;
a.GetObj() = A(n);
cout << a.val<< endl;
}
return 0;
}