self-destruction

Object-Oriented Suicide or delete this;

萝らか妹 提交于 2019-11-27 03:23:37
问题 The following code compiled with MSVC9.0 runs and outputs Destructor four times, which is logical. #include <iostream> class SomeClass { public: void CommitSuicide() { delete this; } void Reincarnate() { this->~SomeClass(); new (this) SomeClass; } ~SomeClass() { std::cout << "Destructor\n"; } }; int main() { SomeClass* p = new SomeClass; p->CommitSuicide(); p = new SomeClass; p->Reincarnate(); p->~SomeClass(); //line 5 p->CommitSuicide(); } I think the first 4 lines of code in main do not

How can I make my .NET application erase itself?

人走茶凉 提交于 2019-11-27 01:05:59
问题 How can I make my C# app erase itself (self-destruct)? Here's two ways that I think might work: Supply another program that deletes the main program. How is this deleter program deleted then, though? Create a process to CMD that waits a few seconds then deletes your file. During those few seconds, you close your application. Both of those methods seem inefficient. I have a feeling that there's some built-in flag or something in Windows that allows for such stuff. How should I do it? Also, can

Self-destructing application

坚强是说给别人听的谎言 提交于 2019-11-26 23:27:15
问题 Along the lines of "This tape will self-destruct in five seconds. Good luck, Jim" ... Would it be possible for an application to delete itself (or it's executable wrapper form) once a preset time of use or other condition has been reached? Alternatively, what other approaches could be used to make the application useless? The aim here is to have a beta expire, inviting users to get a more up-to-date version. 回答1: It is possible. To get around the lock on the JAR file, your application may

What is the use of “delete this”?

一个人想着一个人 提交于 2019-11-26 19:57:56
问题 Today, I have seen some legacy code. In the destructor there is a statement like " delete this ". I think, this call will be recursive. Why it is working? I made some quick search on Y!, I found that if there is a need to restrict the user to create the stack object, we can make destructor private and provide an interface to delete the instance. In the interface provided, we have to call delete on this pointer. Are there any other situations for using such statements? 回答1: "delete this" is

Should “delete this” be called from within a member method?

扶醉桌前 提交于 2019-11-26 18:24:11
问题 I was just reading this article and wanted SO folks advice: Q: Should delete this; be called from within a member method? 回答1: Normally this is a bad idea, but it's occasionally useful. It's perfectly safe as long as you don't use any member variables after you delete, and as long as clients calling this method understand it may delete the object. A good example of when this is useful is if your class employs reference counting: void Ref() { m_References++; } void Deref() { m_References--; if

Should objects delete themselves in C++?

懵懂的女人 提交于 2019-11-26 15:59:34
问题 I've spent the last 4 years in C# so I'm interested in current best practices and common design patterns in C++. Consider the following partial example: class World { public: void Add(Object *object); void Remove(Object *object); void Update(); } class Fire : Object { public: virtual void Update() { if(age > burnTime) { world.Remove(this); delete this; } } } Here we have a world responsible for managing a set of objects and updating them regularly. Fire is an an object that might be added to

Is it safe to `delete this`? [duplicate]

∥☆過路亽.° 提交于 2019-11-26 07:36:47
问题 This question already has answers here : Is delete this allowed? (10 answers) Closed 2 years ago . In my initial basic tests it is perfectly safe to do so. However, it has struck me that attempting to manipulate this later in a function that delete s this could be a runtime error. Is this true, and is it normally safe to delete this ? or are there only certain cases wherein it is safe? 回答1: delete this is legal and does what you would expect: it calls your class's destructor and free the

Is delete this allowed?

坚强是说给别人听的谎言 提交于 2019-11-25 22:36:52
问题 Is it allowed to delete this; if the delete-statement is the last statement that will be executed on that instance of the class? Of course I\'m sure that the object represented by the this -pointer is new ly-created. I\'m thinking about something like this: void SomeModule::doStuff() { // in the controller, \"this\" object of SomeModule is the \"current module\" // now, if I want to switch over to a new Module, eg: controller->setWorkingModule(new OtherModule()); // since the new \