#include <iostream>
using namespace std;
class Test{
int id;
public:
Test(int i): id(i) {
cout << "obj_" << id << " created\n";
}
Test& operator= (const Test& right){
if(this == &right) cout << "same obj!\n";
else {
cout << "obj_" << id << "= obj_" << right.id << endl;
this->id = right.id;
}
return *this;
}
};
int main(){
Test a(1), b(2);
cout << "a = a: ";
a = a; // a.operator = (a);
cout << "a = b: ";
a = b; // a.operator = (b);
return 0;
}
MOOC清华《面向对象程序设计》第3章:赋值运算符重载实验
最新推荐文章于 2023-05-05 09:15:57 发布