调用析构函数然后构造函数(重置对象) [英] Call destructor and then constructor (resetting an object)

查看:152
本文介绍了调用析构函数然后构造函数(重置对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重置对象。我可以用下面的方式做它吗?

I want to reset an object. Can I do it in the following way?

anObject->~AnObject();
anObject = new(anObject) AnObject();
// edit: this is not allowed: anObject->AnObject();

这段代码显然是一个由placement new分配的对象的典型生命周期的子集: p>

This code is obviously a subset of typical life cycle of an object allocated by in placement new:

AnObject* anObject = malloc(sizeof(AnObject));
anObject = new (anObject) AnObject(); // My step 2.
// ...
anObject->~AnObject(); // My step 1.
free(anObject)
// EDIT: The fact I used malloc instead of new doesn't carry any meaning

唯一改变的是构造函数和析构函数调用的顺序。

The only thing that's changed is the order of constructor and destructor calls.

a href =http://yosefk.com/c++fqa/dtor.html#fqa-11.9 =nofollow>以下常见问题
所有威胁会出现?

So, why in the following FAQ all the threatening appear?


[11.9]但是如果我已经分配了对象
给new,我可以显式调用
析构函数

[11.9] But can I explicitly call a destructor if I've allocated my object with new?

常见问题:您不能,除非对象是
分配了placement new。对象
创建的新的必须删除,其中
做两件事情(记住他们):调用
析构函数,然后释放内存。

FAQ: You can't, unless the object was allocated with placement new. Objects created by new must be deleted, which does two things (remember them): calls the destructor, then frees the memory.

FQA:Translation:delete是一种通向
的方式,显式地调用析构函数,但它
也释放内存。你可以
也调用析构函数而不用
释放内存。这很丑陋,
在大多数情况下没用,但你可以做

FQA: Translation: delete is a way to explictly call a destructor, but it also deallocates the memory. You can also call a destructor without deallocating the memory. It's ugly and useless in most cases, but you can do that.

析构函数/显然是正常的C ++代码。代码中使用的保证直接来自于放置新保证。它是标准的核心,这是坚实的事情。如何被称为肮脏,并被表现为不可靠的东西?

The destructor/constructor call is obviously normal C++ code. Guarantees used in the code directly result from the in placement new guarantees. It is the core of the standard, it's rock solid thing. How can it be called "dirty" and be presented as something unreliable?

你认为这是可能的,in-placement和non-in-placement实现new是不同的?我正在考虑一些可能性,常规的新例如可以放置在块之前分配的内存块的大小,这是in-placement新显然不会这样做(因为它不分配任何内存)。

Do you think it's possible, that the in-placement and non-in-placement implementation of new are different? I'm thinking about some sick possibility, that the regular new can for example put size of the memory block allocated before the block, which in-placement new obviously would not do (because it doesn't allocate any memory). This could result in a gap for some problems... Is such new() implementation possible?

推荐答案

可能会导致一些问题出现差距... p>不要被FQA巨魔吸住。像往常一样,他得到的事实是错误的。

Don't get sucked in by the FQA troll. As usual he gets the facts wrong.

你可以直接调用析构函数,对于所有对象,无论他们是创建与放置新。丑陋是在持有人的眼中,它确实很少需要,但唯一困难的事实是内存分配和对象创建必须平衡。

You can certainly call the destructor directly, for all objects whether they are created with placement new or not. Ugly is in the eye of the beholder, it is indeed rarely needed, but the only hard fact is that both memory allocation and object creation must be balanced.

Regularnew / delete通过将内存分配和对象创建结合在一起简化了这一点,堆栈分配甚至进一步简化了它们。

"Regular" new/delete simplifies this a bit by tying memory allocation and object creation together, and stack allocation simplifies it even further by doing both for you.

但是,以下是完全合法的:

However, the following is perfectly legal:

int foo() {
    CBar bar;
    (&bar)->~CBar();
    new (&bar) CBar(42);
 }

两个对象都被销毁,堆栈内存也被自动回收。但与FQA声明不同,析构函数的第一次调用之前没有放置新的。

Both objects are destroyed, and the stack memory is automatically recycled too. yet unlike the FQA claims, the first call of the destructor is not preceded by placement new.

这篇关于调用析构函数然后构造函数(重置对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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