【临时对象返回值优化】
#不开启返回值优化。
#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 用匿名对象初始化同类型,不会调用拷贝构造优化
-
只有一个对象对另一个同类型的对象进行初始化才会调用拷贝构造函数,但是匿名对象对另一个同类型的对象初始化不会调用拷贝构造函数,因为c++编译器对这种情况进行优化,直接将匿名对象转化为该对象,不需要进行额外的内存分配,提高了效率;
-
如果匿名对象对另一个同类型的对象赋值(非初始化),则匿名对象赋值给另一个对象后,匿名对象会被析构。
#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