原始代码
1 #include<iostream> 2 using namespace std; 3 class Test { 4 public: 5 //以参数列表形式对数据成员进行初始化 6 Test(int d = 0) :data(d) 7 { 8 cout << "Create Test Object:" << this << endl; 9 } 10 Test(const Test &t) 11 { 12 cout << "Copy Create Test Object:" << this << endl; 13 this->data = t.data; 14 } 15 Test& operator=(const Test &t) 16 { 17 cout << "Assign:" << this << "=" << &t << endl; 18 if (this != &t) 19 { 20 this->data = t.data; 21 } 22 return *this; 23 } 24 ~Test() 25 { 26 cout << "Free Test Object:" << this << endl; 27 } 28 int GetData() 29 { 30 return data; 31 } 32 private: 33 int data; 34 }; 35 36 Test fun(Test t) 37 { 38 int value = t.GetData(); 39 Test tmp(value); 40 return tmp; 41 } 42 43 int main(int argc, char **argv) 44 { 45 Test t1(10); 46 Test t2 ; 47 t2 = fun(t1); 48 return 0; 49 }