MOOC清华《面向对象程序设计》第3章:赋值运算符重载实验

来源:互联网 发布:linux中vi命令详解 编辑:程序博客网 时间:2024/06/05 19:23
#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;}

阅读全文
0 0