gpt4 book ai didi

c++ - 使用 placement new 时删除它的替代方法

转载 作者:搜寻专家 更新时间:2023-10-31 01:36:00 25 4
gpt4 key购买 nike

我读了here那个

As long as you’re careful, it’s okay (not evil) for an object to commit suicide (delete this).

Here’s how I define “careful”:

You must be absolutely 100% positively sure that this object was allocated via new (not by new[], nor by placement new, nor a local object on the stack, nor a namespace-scope / global, nor a member of another object; but by plain ordinary new).

如果我使用 placement new,我有什么选择?

最佳答案

Placement new 用于分隔内存分配和对象生命周期,因此您通常会显式调用析构函数:

myobj->~myclass();

稍后释放内存,或继续将内存用于其他目的。

关于c++ - 使用 placement new 时删除它的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36404622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com