【临时对象返回值优化】

编程入门 行业动态 更新时间:2024-10-24 04:31:26

【临时对象<a href=https://www.elefans.com/category/jswz/34/1769942.html style=返回值优化】"/>

【临时对象返回值优化】

#不开启返回值优化。

#include <iostream>
using namespace std;
class Rational{
public:Rational() {cout << this << " called Construct" << endl;}~Rational() {cout << this << " called destruct" << endl;}Rational (const Rational& rhs) {cout << this << " copy construct" << endl;}
};
const Rational func() {Rational x = Rational();cout <<  "the add of x " << &x << endl;return x;
}
int main() {func();return 0;
}
0xf2127ffc5f called Construct 创建x 对象
the add of x 0xf2127ffc5f
0xf2127ffcaf copy construct 构造拷贝函数,创建匿名临时对象
0xf2127ffc5f called destruct 销毁x对象
0xf2127ffcaf called destruct 销毁匿名对象
#include <iostream>
using namespace std;
class Rational{
public:Rational() {cout << this << " called Construct" << endl;}~Rational() {cout << this << " called destruct" << endl;}Rational (const Rational& rhs) {cout << this << " copy construct" << endl;}
};
const Rational func() {Rational x = Rational();cout <<  "the add of x " << &x << endl;return x;
}
int main() {Rational y = func();cout << "the add of y " << &y << endl;return 0;
}
0xd6d19ffc3f called Construct 创建x
the add of x 0xd6d19ffc3f
0xd6d19ffc8f copy construct    创建匿名临时变量,
0xd6d19ffc3f called destruct 销毁x
the add of y 0xd6d19ffc8f
0xd6d19ffc8f called destruct 销毁y  用匿名对象初始化同类型,不会调用拷贝构造优化
  1. 只有一个对象对另一个同类型的对象进行初始化才会调用拷贝构造函数,但是匿名对象对另一个同类型的对象初始化不会调用拷贝构造函数,因为c++编译器对这种情况进行优化,直接将匿名对象转化为该对象,不需要进行额外的内存分配,提高了效率;

  2. 如果匿名对象对另一个同类型的对象赋值(非初始化),则匿名对象赋值给另一个对象后,匿名对象会被析构。

#include <iostream>
using namespace std;
class Rational{
public:Rational() {cout << this << " called Construct" << endl;}~Rational() {cout << this << " called destruct" << endl;}Rational (const Rational& rhs) {cout << this << " copy construct" << endl;}
};
const Rational func() {return Rational();
}
int main() {Rational y = func();cout << "the add of y " << &y << endl;return 0;
}

#include <iostream>
using namespace std;
class Rational{
public:Rational() {cout << this << " called Construct" << endl;}~Rational() {cout << this << " called destruct" << endl;}Rational (const Rational& rhs) {cout << this << " copy construct" << endl;}
};
const Rational func() {return Rational();
}
int main() {Rational x;Rational y = x;   // 会调用copy 构造函数cout << "the add of y " << &y << endl;Rational z;z = x;  // 浅拷贝,不调用copy 构造函数Rational w(x);//调用copy 构造函数return 0;
}

```cpp
0x6ff1ffcaf called Construct 创建x
0x6ff1ffcae copy construct 调用copy construct 创建y
the add of y 0x6ff1ffcae
0x6ff1ffcad called Construct 调用construct 创建z
0x6ff1ffcac copy construct  调用copy construct 创建w
0x6ff1ffcac called destruct 销毁w
0x6ff1ffcad called destruct 销毁z
0x6ff1ffcae called destruct 销毁y
0x6ff1ffcaf called destruct 销毁x
#不开启返回值优化。
```cpp
#include <iostream>
using namespace std;
class Rational{
public:Rational() {cout << this << " called Construct" << endl;}~Rational() {cout << this << " called destruct" << endl;}Rational (const Rational& rhs) {cout << this << " copy construct" << endl;}
};
const Rational func() {Rational x = Rational();cout <<  "the add of x " << &x << endl;return x;
}
int main() {func();return 0;
}
0xf2127ffc5f called Construct 创建x 对象
the add of x 0xf2127ffc5f
0xf2127ffcaf copy construct 构造拷贝函数,创建匿名临时对象
0xf2127ffc5f called destruct 销毁x对象
0xf2127ffcaf called destruct 销毁匿名对象

#开启返回值优化后

#include <iostream>
using namespace std;
class Rational{
public:Rational() {cout << this << " called Construct" << endl;}~Rational() {cout << this << " called destruct" << endl;}Rational (const Rational& rhs) {cout << this << " copy construct" << endl;}
};
const Rational func() {Rational x = Rational();cout <<  "the add of x " << &x << endl;return x;
}
int main() {func();return 0;
}

优化了匿名函数的copy 构造和析构

0x340bfff92f called Construct
the add of x 0x340bfff92f
0x340bfff92f called destruct

更多推荐

【临时对象返回值优化】

本文发布于:2023-11-15 23:28:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609437.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!