【临时对象返回值优化】

#不开启返回值优化。

#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值