C++ Object-Oriented Programming 007: What should be returned---- (Peking University Mooc)

Article Directory


Original title

Insert picture description here

#include <iostream>
using namespace std;
class A {
    
    
public:
	int val;

	A(int
// 在此处补充你的代码
};
int main()
{
    
    
	int m,n;
	A a;
	cout << a.val << endl;
	while(cin >> m >> n) {
    
    
		a.GetObj() = m;
		cout << a.val << endl;
		a.GetObj() = A(n);
		cout << a.val<< endl;
	}
	return 0;
}


Code

	A(){
    
    val = 123;}
    A(int n)
    {
    
    
        val = n;
    }
    A& GetObj()
    {
    
    
        return *this;//去查阅了答案才知道有这个指针
    }

Guess you like

Origin blog.csdn.net/qq_37500516/article/details/114704527