- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下 C++ 代码 (VS2013):
#include <iostream>
using namespace std;
class A {
int i;
public:
A(int i) : i(i) {
cout << "DEFAULT CTOR " << i << endl;
}
A(const A &o) : i(o.i) {
cout << "COPY CTOR " << i << endl;
}
~A() {
cout << "DTOR " << i << endl;
}
friend A f(const A &, A, A *);
};
A f(const A &a, A b, A *c) {
return *c;
}
int main() {
f(1, A(2), &A(3));
}
它产生以下输出:
DEFAULT CTOR 1
DEFAULT CTOR 3
DEFAULT CTOR 2
COPY CTOR 3
DTOR 2
DTOR 3
DTOR 3
DTOR 1
前 3 个是参数构造函数(错误地输出“DEFAULT CTOR”,但这无关紧要),它们在调用 f
之前被调用。
然后,当运行 return *c;
行时,运行值为 3 的复制构造函数,然后销毁值为 2 的对象。
最后,在 main
范围的末尾,剩余的对象 (3, 3, 1) 被销毁。
我不明白这种行为,也找不到任何解释。
谁能详细说明事情发生的顺序?
具体来说:
为什么第三个对象&A(3)
构造在第二个对象A(2)
之前?这和它们的创建有什么关系吗(第三个是引用,第二个是值),还是f
的定义方式(第二个是值,第三个是指针) ?
当 return *c;
运行时,将创建第三个对象的拷贝以供返回。然后,在返回之前唯一被破坏的对象是第二个对象。同样,这与它们的创建或 f
的定义方式有什么关系吗?
提前致谢。
最佳答案
Why is the third object &A(3) constructed before the second object A(2)? Does this have anything to do with their creation (the third is by reference, the second is by value), or with the way f is defined (the second is by value, the third is a pointer)?
这是因为 C++ 标准未指定函数参数的计算顺序。编译器可以随心所欲地评估它们。这是许多未定义行为实例的原因,当不知道这一点的程序员依赖于不存在的顺序时。
When return *c; runs, a copy of the third object is created to be returned. Then, the only object being destructed before returning is the second object. Again, does this have anything to do with their creation, or with the way f is defined?
有点,是的。 A(2)
对象是通过直接初始化函数 f
的参数创建的。函数参数的范围是函数体。所以 A(2)
在函数退出的那一刻就超出了范围。其他对象的生命周期稍长,因为它们是在函数外部创建的,并通过引用/指针传递。它们一直存在到完整表达式 f(1, A(2), &A(3));
的末尾,因此它们稍后会被破坏。
关于c++ - 构造和破坏的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44900289/
我正在尝试完成撤消/重做。我正在使用loadFromJSON(...)从我存储在数组中的 Canvas 状态重新构建 Canvas 。基本上,我的想法是破坏现有的 Canvas 并重新构建 Canva
在某些情况下,我有一个在 iframe 中打开的网页。当它被加载到那个 iframe 中时,我需要它将窗口位置设置为资源以下载文件(所有这些都是为了更新 GreaseMonkey 脚本......所有
当我创建 Intent 时: Intent in = new Intent(this, myclass.class); this.startActivity(in); 我创建了一个新的 Intent
我正在我本地版本的 Wordpress 网站上为 Wordpress 创建新的短代码。 在 functions.php 中,我添加了例如: function shortTest() { re
我正在为机械网站制作 JavaScript 闪卡游戏。因为我想将方程写在卡片上,所以我需要使用 delta(Δ) 符号。 一张卡片可能有:一侧是“功率方程”,另一侧是“P=W/Δt”。如果卡片从第一面
我编写了以下代码: document.addEventListener("DOMContentLoaded", ()=>{ let menu = document.querySelector(
我的浏览器同步工作正常,但我仍然很难处理之前的 html 的缓存。即使选中了 Chrome 的“禁用缓存”,甚至在隐身模式下也是如此! 要加载页面更改,我总是必须“清除缓存并硬重新加载”。 我想知道,
我注意到每次打开和关闭(通过单击菜单项或单击菜单外的某个区域)时,上下文菜单 ( Ext.menu.Menu ) s 不会从 DOM 中删除,它们只是以某种方式变得不可见。 如何改变这个? 最佳答案
给定依赖记录类型: Record FinPath : Type := mkPath { fp_head : S i; fp_tail
在 Husdon/Jenkins 中,我可以在构建被破坏时设置通知,以向进行破坏构建的 checkin 的用户发送电子邮件。如何在 Teamcity 中执行此操作? 我知道个人用户可以通过 Teamc
我注意到每次打开和关闭(通过单击菜单项或单击菜单外的某个区域)时,上下文菜单 ( Ext.menu.Menu ) s 不会从 DOM 中删除,它们只是以某种方式变得不可见。 如何改变这个? 最佳答案
使用 MIMEMultipart('alternative') 发送 html 和 pain-text 时 将 html 转换为文本时,html 的 anchor 换行 http://127.0.0.
每当我的应用程序最小化时,我都会启动一个服务,该服务向我的 HTTP 服务器发送拉取请求以检查通知,当应用程序恢复时,服务将被终止(以及计划的可运行项)。一切正常,直到我决定终止该应用程序(将其从正在
我意识到该框架处于 alpha 阶段,但正在实现 jQuery Mobile破坏了我的omniauth 身份验证。当我尝试登录时,一旦我尝试点击/auth/twitter Controller ,jQ
我对 Angular 比较陌生,经过几个小时的调试,我发现添加 jquery 时存在一些不兼容性。该指令在没有 jquery 的情况下工作正常,但在使用 jquery 时会中断:/ 这是一个 plnk
我发现,因为我正在处理的所有表单都有一个包含“name =“submit””属性的提交按钮,所以当我单击应该触发表单提交的链接时,触发器提交会中断. 有谁知道我该如何解决这个问题。 下面的 JQuer
我遇到了一个问题:/我得到了一个 CSS 东西,它使悬停时背景位置发生变化。但是当我在 javascript 中运行一个改变悬停的函数后,CSS 停止工作。 这是函数: function tree()
谁能给出一个完整的例子来说明 qooxdoo 1.6 中的 dispose 和 destruct 是如何工作的? ,我在 qooxdoo 演示或文档中找不到任何好的示例。 谢谢你的建议。 最佳答案 处
我对 JFormattedTextField 有疑问(我将它用作我们所有文本字段的基类)。 今天我尝试向该字段的文档添加一个文档过滤器,它工作得很好,但前提是它没有设置格式化程序工厂。 问题是,当设置
我有一个点击事件 $('#ship_Move').click(function (event) { event.stopPropagation();
我是一名优秀的程序员,十分优秀!