是“删除此”声明有效吗? [英] Is "delete this" statement valid?

查看:108
本文介绍了是“删除此”声明有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



最近我在接受采访时向我询问了一个问题。

问题是:声明是否有效?



Hi All,

Recently a question has been asked to me in an interview.
The question was : Is the statement valid?

delete this;





可以有人请告诉我,如果这是有效的或有效的(有理由的话)?



据我所知,这是一种自杀,我们不应该使用这些陈述。但是根据多线程应用中的采访者,这是可能的。但我不确定如何。



请回答。



提前致谢



Can someone please let me know, if this is valid or not (with reason)?

As per me, this is a kind of suicide and we should not use such statements. But as per interviewer in multithreaded application it is possible.But i am not sure how.

Please answer.

Thanks in advance

推荐答案

对象可以自行删除吗?是的它可以。



此外,你知道引用计数的技巧吗?在许多其他事情中,它用于COM。通过引用计数,这是删除对象和回收堆内存的标准技术:对象的每个用户都应调用其方法递增引用计数器,并在使用后调用递减它的方法。这两种方法中的最后一种应该以您询问的方式删除对象。







这是解释引用计数技术并使用删除此的众多文章之一。不要害怕它,享受!



干杯,

-SA
Can the object delete itself? Yes it can.

Moreover, do you know the technique of reference counting? Among many other things, it is used in COM. With reference counting, this is a standard technique of deleting objects and reclaiming heap memory: every user of object should call its method incrementing a reference counter, and, after use, call the method decrementing it. The last of these two methods is supposed to delete the object in the way you are asking about.



This is one of the many articles explaining the reference counting technique and using delete this. Don''t be afraid of it, enjoy!

Cheers,
—SA


完全正常 [ ^ ]。



事实上,微软甚至建议使用它来自毁Windows对象这里 [ ^ ](在页面上搜索删除此)
It is perfectly normal[^].

In fact, Microsoft even suggest using it for self-destroying Window objects here[^] (search "delete this" on the page)


是,删除此项;已经定义了结果,只要你确保对象是动态分配的,并且(当然)在它被销毁后永远不会尝试使用该对象。



认为这是一个讨厌的黑客,并告诉任何人谁会听,应该避免。一个常被引用的问题是难以确保仅动态分配类的对象。其他人认为这是一个非常合理的习惯用法,并且一直使用它。就个人而言,我在中间的某个地方:我很少使用它,但是当它似乎是适合这项工作的工具时,不要犹豫。



编辑:[主要是为了响应@Alexandre C'的评论]:你这样做的主要时间是一个生命几乎完全属于自己的对象。 James Kanze引用的一个例子是他为电话公司工作的计费/跟踪系统。基本上,当你拿起手机时,会注意到这一点并创建一个phone_call对象。从那时起,phone_call对象处理电话呼叫的详细信息(在拨号时建立连接,向数据库添加条目以说明呼叫何时开始,如果您进行电话会议,可能会连接更多人等)当你挂断电话时,phone_call对象会进行最后的簿记(例如,在数据库中添加一个条目,说明你什么时候挂机,这样他们就可以计算你的通话时间),然后自行销毁。 phone_call对象的生命周期取决于你拿起/挂断电话的时间 - 从系统其他部分的角度来看,它基本上是完全随意的,所以你不能把它绑在任何词法范围内在代码中,或者在该订单上的任何内容。



对于任何可能关心这种编码可靠性的人:如果你拨打电话,来自或者通过欧洲几乎任何一个地方,很有可能它正在通过完全相同的代码处理(至少部分)。
Yes, delete this; has defined results, as long as you assure the object was allocated dynamically, and (of course) never attempt to use the object after it''s destroyed.

Some consider this a nasty hack, and tell anybody who will listen that it should be avoided. One commonly cited problem is the difficulty of ensuring that objects of the class are only ever allocated dynamically. Others consider it a perfectly reasonable idiom, and use it all the time. Personally, I''m somewhere in the middle: I rarely use it, but don''t hesitate to do so when it seems to be the right tool for the job.

[mostly in response to @Alexandre C''s comment]: the primary time you do this is with an object that has a life that''s almost entirely its own. One example James Kanze has cited was a billing/tracking system he worked on for a phone company. Basically, when you pick up the phone, something takes note of that and creates a phone_call object. From that point onward, the phone_call object handles the details of the phone call (making a connection when you dial, adding an entry to the database to say when the call started, possibly connect more people if you do a conference call, etc.) When you hang up, the phone_call object does its final book-keeping (e.g., adds an entry to the database to say when you hung up, so they can compute how long your call was) and then destroys itself. The lifetime of the phone_call object is based on when you pick up/hang up the phone -- from the viewpoint of the rest of the system, it''s basically entirely arbitrary, so you can''t tie it to any lexical scope in the code, or anything on that order.

For anybody who might care about how dependable this kind of coding can be: if you make a phone call to, from, or through almost any part of Europe, there''s a pretty good chance that it''s being handled (at least in part) by code that does exactly this.


这篇关于是“删除此”声明有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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