标签:idt 临时 view namespace div color 数列 opened http
原始代码
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 }
有结果观察可知,fun函数里面,tmp这个临时对象被创建的意义仅仅是为了调用拷贝构造函数初始化一个临时对象。初始化临时对象完毕后就被析构了。
代码优化,创建无名临时对象
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 return Test(value);//创建无名临时对象 40 } 41 42 int main(int argc, char **argv) 43 { 44 Test t1(10); 45 Test t2 ; 46 t2 = fun(t1); 47 return 0; 48 }
fun函数return返回那里没有再调用拷贝构造函数
代码继续优化,fun函数参数改为传引用
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 return Test(value);//创建无名临时对象 40 } 41 42 int main(int argc, char **argv) 43 { 44 Test t1(10); 45 Test t2 ; 46 t2 = fun(t1); 47 return 0; 48 }
在继续优化,fun函数返回引用
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 return Test(value);//创建无名临时对象 40 } 41 42 int main(int argc, char **argv) 43 { 44 Test t1(10); 45 Test t2 ; 46 t2 = fun(t1); 47 return 0; 48 }
这段代码在VS2017下编译不过,分析原因如下
return Test(value);//创建无名临时对象
无名临时对象出了函数就被释放了,t2 = fun(t1);调用赋值函数,是将一个已被析构的对象赋值给具体对象t2。
函数返回值能否写成引用,要看所引用的对象是否是局部对象,如果出了函数对象被析构了就不要引用。
代码继续优化
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 return Test(value);//创建无名临时对象 40 } 41 42 int main(int argc, char **argv) 43 { 44 Test t1(10); 45 Test t2 = fun(t1); 46 return 0; 47 }
标签:idt 临时 view namespace div color 数列 opened http
原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9442408.html