【临时对象返回值优化】

#不开启返回值优化。

#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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/134704.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何进行单病种质控上报管理

过程质量管理发展历程 单病种质量管理兴起之初&#xff0c;医疗机构多强调致残率、致死率、平均住院日、治愈好转率等结果性指标。这些指标主观性强&#xff0c;且为事后管理&#xff0c;无法及时发现问题&#xff0c;具有滞后性。 《卫生部办公厅关于开展单病种质量管理控制…

vue开发环境搭建部署(mac版)

前言 目前后端工作越来越少了&#xff0c;年底了&#xff0c;为了先过验收。项目负责人、产品、需求制定的方案就是先做假页面&#xff0c;所以前端的活多点。 其实现在不喜欢搞前端&#xff0c;原因很多&#xff0c;但是感觉现在似乎流行的码林绝学又是九九归一的瓶颈期…

【C#】文件的移动

今天遇到一个问题&#xff0c;是有关文件移动的&#xff0c;比较棘手&#xff0c;刚解决完就立马发篇文章保存下来&#xff0c;一方面自己看&#xff0c;一方面给大家分享一下。 首先在我的Unity项目里&#xff0c;资源图片文件夹里面大概有400多张图&#xff0c;分别在各自命…

Vue实现面经基础版案例(路由+组件缓存)

一、面经基础版-案例效果分析 1.面经效果演示 2.功能分析 通过演示效果发现&#xff0c;主要的功能页面有两个&#xff0c;一个是列表页&#xff0c;一个是详情页&#xff0c;并且在列表页点击时可以跳转到详情页底部导航可以来回切换&#xff0c;并且切换时&#xff0c;只有…

掌握未来:PureBasic for Mac引领BASIC语言编辑器的新潮流

PureBasic for Mac是一种创新的BASIC语言编辑器&#xff0c;它赋予了编程更多的可能性。在这个充满机遇的时代&#xff0c;掌握编程就等于掌握了一种强大的工具&#xff0c;能够更好地理解和塑造世界。而PureBasic for Mac&#xff0c;正是这样一个让你轻松上手&#xff0c;高效…

蓝桥杯官网练习题(正则问题)

题目描述 考虑一种简单的正则表达式&#xff1a; 只由 x ( ) | 组成的正则表达式。 小明想求出这个正则表达式能接受的最长字符串的长度。 例如 ((xx|xxx)x|(x|xx))xx 能接受的最长字符串是&#xff1a; xxxxxx&#xff0c;长度是 6。 输入描述 一个由 x()| 组成的正则表…

Docker学习路线

一、Docker基础 1. docker的安装 先在虚拟机中安装Linux。 VirtualBox 虚拟机 在CentOS中安装Docker 安装Docker 2. 快速入门 部署 mysql docker部署mysql 3. 常见命令 4. 命令别名 5. 数据卷挂载 6. 本地目录挂载 7. Dokerfile语法 8. 自定义镜像 9. 容器网络互连 …

如何开发一个求职招聘小程序?详细步骤解析与教程

一、确定需求和功能 在开发求职招聘小程序之前&#xff0c;需要明确需求和功能。通过对市场和用户需求的调研和分析&#xff0c;确定小程序需要具备哪些功能&#xff0c;如职位发布、简历投递、在线沟通、面试安排等。 二、选择开发方式 求职招聘小程序的开发方式有多种选择…

【C#枚举 Enum】

C#枚举 Enum 一、枚举 1、枚举是将变量的值罗列出来,变量的值只限于列举出来的值的范围。 2、枚举使用enum关键字来声明&#xff0c;与类同级。枚举本身可以有修饰符&#xff0c;但枚举的成员始终是公共的&#xff0c;不能有访问修饰符。枚举本身的修饰符仅能使用public和int…

Mac安装opencvJava踩坑

SpringBoot导入opencv依赖 先将jar包添加到libraries中在resources目录下创建lib文件夹并复制jar包到这里添加如下依赖&#xff0c;并刷新maven <dependency><groupId>org.opencv</groupId><artifactId>opencv</artifactId><version>4.8.0…

【理解链表指针赋值】链表中cur->next = cur->next->next->next与cur =cur->next->next的区别

最近在做链表的题目的时候&#xff0c;对于所定义的cur链表指针产生了一些疑惑&#xff0c;查阅资料后整理一下我的理解&#xff1a; /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(n…

Android 常用 UI 组件

目录 ​编辑 1. View 和ViewGroup 2. Android UI 开发概述 2.1 界面布局开发 2.2 控件开发 2.3 AdapterView 与 Adapter 开发 2.4 UI 组件开发 2.5 自定义 View、图形图像和动画 1. View 和ViewGroup Android中所有的UI元素都是使用View和ViewGroup对象建立的,…

java_网路爬虫_2

连接池 package cn.lala.crawler.httpclient.test;import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClien…

汽车标定技术(二)--基于XCP的标定测量实战

目录 1.工程创建 1.1 新建工程 1.2 设备配置 1.3 标定观测 1.4 刷写 2.原始hex文件与标定文件的合并 2.1 修改memory segment file 2.2 标定量地址偏移 ​编辑 2.3 标定后与原始hex文件合并 2.4 标定后直接merge 2.5 不用对ram地址进行偏移实现hex文件合并 本文使用…

Jdk 1.8 for mac 详细安装教程(含版本切换)

Jdk 1.8 for mac 详细安装教程&#xff08;含版本切换&#xff09; 官网下载链接 https://www.oracle.com/cn/java/technologies/downloads/#java8-mac 一、选择我们需要安装的jdk版本&#xff0c;这里以jdk8为例&#xff0c;下载 macOS 版本&#xff0c;M芯片下载ARM64版本…

Google Guava Cache LoadingCache 基本使用

一. 添加依赖 <dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>27.1-jre</version> </dependency>二. 创建CacheLoader LoadingCache<Long, String> cache CacheBuilder.newB…

Presentation Prompter 5.4.2(mac屏幕提词器)

Presentation Prompter是一款演讲辅助屏幕提词器软件&#xff0c;旨在帮助演讲者在公共演讲、主持活动或录制视频时更加流畅地进行演讲。以下是Presentation Prompter的一些特色功能&#xff1a; 提供滚动或分页显示&#xff1a;可以将演讲稿以滚动或分页的形式显示在屏幕上&a…

【Solidity】Remix在线环境及钱包申请

好久没有学习区块链方面的知识了&#xff0c;目前通过自学大致掌握了Fabric联盟链的搭建&#xff0c;链码编写、部署&#xff0c;api调用&#xff0c;可以独立开发出一些基于fabric的应用&#xff0c;感觉开发出去中心化的应用还是很有意思的&#xff0c;因为他与之前开发的ssm…

数据集:机器人理解世界的关键

原创 | 文 BFT机器人 传统的机器人和工业自动化解决方案已经颇有成效。在工厂中入驻自动化机器人可以快速地帮助工人们完成长时间重复劳动的任务。随着用工成本上涨、技能人才短缺、工作环境恶劣等问题的凸显&#xff0c;社会更迫切地需要采用自动化设备代替人工来完成该类操作…

Pod控制器详解

目录 一、Pod控制器及其功用 二、Deployment控制器 三、StatefulSet控制器 四、DaemonSet控制器 五、Job控制器 六、CronJob 控制器 一、Pod控制器及其功用 Pod控制器&#xff0c;又称之为工作负载&#xff08;workload&#xff09;&#xff0c;是用于实现管理pod的中间层…