小标
2018-12-03
来源 :
阅读 1228
评论 0
摘要:本文主要向大家介绍了 C/C++知识点之C++——函数调用优化,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了 C/C++知识点之C++——函数调用优化,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
1 #include
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
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
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
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
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
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号