C++——调用优化

原始代码

 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 }
View Code

有结果观察可知,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 }
View Code

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 }
View Code

在继续优化,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 }
View Code

这段代码在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 }
View Code

 

转载于:https://www.cnblogs.com/kelamoyujuzhen/p/9442408.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值