检查是否删除了对象 [英] Check If Object Deleted

查看:51
本文介绍了检查是否删除了对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想检查我的对象是否已被删除,但是我的

程序一直在崩溃。这里是简化的代码(无限循环)


删除tetromino;

// if(tetromino == NULL)

tetromino =新的TetrominoI;


这会不断取代预期的tetromino,但是如果我拿走

那么评论就会崩溃。我怎样才能检查我的物品是否被删除了?
被删除了?


谢谢

Hi,

I would like to check if my object has been deleted or not, but my
program keeps crashing. Here''s the simplified code (in infinite loop)

delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;

This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?

Thanks

推荐答案

Mark写道:
Mark wrote:




我想检查一下我的对象已删除或未删除,但我的

程序一直在崩溃。这里是简化的代码(无限循环)


删除tetromino;

// if(tetromino == NULL)

tetromino =新的TetrominoI;


这会不断取代预期的tetromino,但是如果我拿走

那么评论就会崩溃。如何检查我的对象是否已被删除?b / b
Hi,

I would like to check if my object has been deleted or not, but my
program keeps crashing. Here''s the simplified code (in infinite loop)

delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;

This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?



你不能,除非你把指针设置为删除

之后为NULL,或者对象的析构函数设置某种静态标志以表示

它已被删除。


-

Ian Collins。

You can''t, unless you either set the pointer to NULL after you delete
it, or the object''s destructor sets some kind of static flag to indicate
it has been deleted.

--
Ian Collins.


Mark写道:
Mark wrote:




我想检查我的对象是否已被删除,但是我的

程序一直在崩溃。这里是简化的代码(无限循环)


删除tetromino;

// if(tetromino == NULL)

tetromino =新的TetrominoI;


这会不断取代预期的tetromino,但是如果我拿走

那么评论就会崩溃。如何检查我的对象是否已被删除?b $ b被删除?
Hi,

I would like to check if my object has been deleted or not, but my
program keeps crashing. Here''s the simplified code (in infinite loop)

delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;

This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?



a)你不需要。对delete的调用保证了对象的删除。


b)有些人这样做


删除tetromino;

tetromino = 0;


但是,请注意,如果有另一个指向* tetromino的指针,并且您通过该指针删除了

指针,则测试tetromino = = 0会给你买不到的东西

因为tetromino不会神奇地被淘汰。这个成语在

最好是危险的。


c)你可以找到/实现一个智能指针,只要

,它就会恢复为0 pointee是通过它的任何句柄删除的。


_However_:


d)你应该重新考虑你的设计。你希望检查指针是否被删除是否意味着你的方向错误。

最好


Kai-Uwe Bux

a) You need not. The call to delete guarantees that the object is deleted.

b) Some people do

delete tetromino;
tetromino = 0;

However, note that if there is another pointer to *tetromino, and you delete
the pointee through that one, the test tetromino == 0 will buy you nothing
because tetromino will not magically be nulled. This idiom is dangerous at
best.

c) You can find / implement a smart pointer that reverts to 0 whenever the
pointee is delete through any of its handles.

_However_:

d) You should rethink your design. Your wish to check whether the pointer is
deleted is indicative that your headed the wrong way.
Best

Kai-Uwe Bux


" Mark" < mn ******* @ gmail.com写信息

新闻:11 ********************** @ 57g2000hsv.googlegro ups.com ...
"Mark" <mn*******@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...




我想检查我的对象是否已被删除或不,但我的

程序一直在崩溃。这里是简化的代码(无限循环)


删除tetromino;

// if(tetromino == NULL)

tetromino =新的TetrominoI;


这会不断取代预期的tetromino,但是如果我拿走

那么评论就会崩溃。如何检查我的对象是否已被删除?b $ b被删除?
Hi,

I would like to check if my object has been deleted or not, but my
program keeps crashing. Here''s the simplified code (in infinite loop)

delete tetromino;
//if(tetromino==NULL)
tetromino = new TetrominoI;

This continuously replaces the tetromino as expected, but if I take
away the comment it crashes. How can I check if my object has been
deleted or not?



delete不会改变指针。就在指针指向的位置。如果你想要一个指针在你删除之后指向NULL,你需要这样做

你自己。


删除tetromino;

tetromino = NULL;


if(tetromino == NULL)

tetromino =新的Tetromino;


在这段简短的代码中它没有多大意义,但它可以在一个

级别的heirarchy中。我有时也将它用于静态变量。


静态jTexture * = NULL;

if(jTexture = NULL)

{

//加载纹理

}


和课程。


此外,删除NULL指针是完全安全的。这是一个非操作,没有什么

发生。删除已经被删除的指针会导致

崩溃。

delete does not change the pointer. Just where the pointer points. If you
want a pointer to point to NULL after you delete it, you need to do so
yourself.

delete tetromino;
tetromino = NULL;

if ( tetromino == NULL )
tetromino = new Tetromino;

It doesn''t make much sense in this short piece of code, but it can in a
class heirarchy. I''ve sometimes used it for static variables too.

static jTexture* = NULL;
if ( jTexture = NULL )
{
// load texture
}

and in classes.

Also, it is perfectly safe to delete a NULL pointer. It''s a non-op, nothing
happens. Deleting a pointer that''s already been deleted however will cause
a crash.


这篇关于检查是否删除了对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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