关于运营商新的问题 [英] Question about operator new

查看:88
本文介绍了关于运营商新的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用新的一个对象的放置表单时(实际上它没有为对象分配内存空间
),我该怎么删除这个对象

相应的?

解决方案




codefuns写道:

当我使用新的一个对象的贴图形式(实际上它没有为对象分配内存空间)时,我应该如何相应地删除这个对象?




通过伪析构函数调用。


T对象;


codefuns写道:

当我使用新的一个对象的放置表单时(实际上它没有为对象分配内存空间),我应该如何相应地删除这个对象?




不,你应该只明确地调用它的析构函数。


MyClass * myObj = new(somestorage)MyClass();

...

myObj->〜我的课(); // destroy


存储与''myObj'的生命周期无关,以及你如何获得它

或处理它是无关紧要的。


V





codefuns写道:

当我使用新的一个对象的放置表单时(实际上它没有为对象分配内存空间),我应该如何相应地删除这个对象?




通过伪析构函数调用。


(对不起,按错键......我会继续)


T对象;

对象.~T();

new(& object)T();


但是你为什么要这样做呢?如果你不知道怎么回事,我会怀疑你不应该......


Markus


when I use the placement form new to new a object(actually it does not
alloc memory space for the object), how should I delete this object
accordingly?

解决方案

Hi

codefuns wrote:

when I use the placement form new to new a object(actually it does not
alloc memory space for the object), how should I delete this object
accordingly?



By a pseudo-destructor call.

T object;


codefuns wrote:

when I use the placement form new to new a object(actually it does not
alloc memory space for the object), how should I delete this object
accordingly?



No, you should only call its destructor explicitly.

MyClass *myObj = new (somestorage) MyClass();
...
myObj->~MyClass(); // destroy

The storage is unrelated to the lifetime of ''myObj'' and how you obtain it
or dispose of it is immaterial here.

V


Hi

codefuns wrote:

when I use the placement form new to new a object(actually it does not
alloc memory space for the object), how should I delete this object
accordingly?



By a pseudo-destructor call.

(sorry, hit the wrong key... I''ll continue)

T object;
object.~T();
new (&object) T();

But why do you want to do this in the first place? If you don''t know how, I
suspect you shouldn''t...

Markus


这篇关于关于运营商新的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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