12345678910111213141516171819202122232425262728293031323334353637
class Test { public: int *ptr; Test(int i = 0) { ptr = new int(i); } const Test& operator=(const Test &t) { *ptr = *(t.ptr); return *this; } //this produces the same result, why? // Test operator=(const Test &t) // { // *ptr = *(t.ptr); // return *this; // } }; int main() { Test t1(5); Test t2; t2 = t1; t1.ptr = new int(10); cout << "t2 = " << *(t2.ptr) << endl; return 0; }
12
a = b = c = d; //Assign a, b and c value of d — three objects changed, no copies
while((c = getchar()) != EOF)
123
t2 = t1; t1.ptr = new int(10);//t1 and t2 are the same object cout << "t2 = " << *(t2.ptr) << endl;//but t2.ptr is still 5