对象存在于C ++中是什么意思? [英] What does it mean for an object to exist in C++?

查看:135
本文介绍了对象存在于C ++中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[class.dtor] / 15 read,emphasis mine:

[class.dtor]/15 reads, emphasis mine:


一旦为对象调用析构函数,对象不再存在;

但是,就我自己而言,这个行为是未定义的。告诉,这是标准中对现有对象的唯一引用。这也似乎与[basic.life]对比,它更具体:

However, as far as I can tell, this is the only reference in the standard to an object "existing." This also seems to contrast with [basic.life], which is more specific:


类型<$ c $的对象的生命周期c> T 结束于:


  • if T 是一个类型类型,具有非平凡析构函数(12.4),析构函数调用开始,或

  • if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or

这里有两个不同的用法:对象的生命周期结束和对象不再存在,前者只发生在一个非平凡的析构函数,后者发生在任何析构函数。差异的意义是什么?一个对象不再存在的意义是什么?

We have two different wordings here: "the lifetime of an object ends" and "the object no longer exists," the former only happens with a non-trivial destructor and the latter happens with any destructor. What is the significance of the difference? What is the implication of an object no longer existing?

推荐答案

引用的措辞似乎意味着编译器可以正确插入代码它将与对象相关的内存返回到堆的开始的析构函数。但这样做会消除一个对象在销毁过程中引用自己的成员的能力,如果一个对象能够毁掉自己的话,这是必需的。

The quoted wording would seem to imply that a compiler could correctly insert code that returns the memory associated with an object to the heap at the beginning of its destructor. But doing that would eliminate the ability of an object to reference its own members during destruction, which is required if an object is to be able to destroy itself.

关于生命和存在是什么意思,我建议有一些不同的上下文,它们意味着不同的事物:

Concerning what "lifetime" and "existence" mean, I propose that there are some different contexts, in which they mean different things:

在构造的上下文中,生命和存在在构造函数开始时开始。

Within the context of construction, lifetime and existence begin when a constructor begins. Outside that context, they begin when a constructor ends.

在破坏的上下文中,生命和存在在析构函数结束时结束。

Within the context of destruction, lifetime and existence end when a destructor ends. Outside that context, they end when destruction begins.

因此,一个对象可能在构造期间引用它自己的成员,并且可能将自身传递给其他对象的函数,这可能引用对象及其成员,等等。但是一般来说,对象(类的实例)在它们的构造函数完成之后可能不被引用(不产生未定义的行为)。

So an object may refer to its own members during construction, and potentially pass itself to functions of other objects, which may refer to the object and its members, and so on. But in general, objects (instances of classes) may not be referenced (without producing undefined behavior) until after one of their constructors has finished.

对象的析构函数可以引用到其自己的成员并调用其他(现有的)对象的函数,其可以指被销毁的对象和/或其成员。但是一般来说,一个对象在析构函数开始后可能不会被引用。

And an object's destructor may refer to its own members and call functions of other (existing) objects, which may refer to the object being destroyed and/or its members. But in general, an object may not be referenced after its destructor has started.

这种多上下文定义是对我最有意义的,但我可以看到的参数是一个对象应该被认为是活着的,从分配内存的时刻到内存释放的时刻,我会说,一个浅层副本的内存应该分配给一个对象的一个​​构造函数开始时,并在析构函数结束时释放。

This sort of multi-contextual definition is what makes the most sense to me, but I can see arguments being made that an object should be considered to be alive from the moment memory is allocated for it to the moment that memory is released, and I would say memory for a shallow copy should be allocated for an object when one of its constructors starts, and released when its destructor ends.

这篇关于对象存在于C ++中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆