resource-cleanup

Should objects delete themselves in C++?

旧街凉风 提交于 2019-11-27 12:24:27
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 the world under many different circumstances but typically by another object already in the world. Fire

How can I get around the lack of a finally block in PHP?

筅森魡賤 提交于 2019-11-27 01:32:25
问题 PHP prior to version 5.5 has no finally block - i.e., whereas in most sensible languages, you can do: try { //do something } catch(Exception ex) { //handle an error } finally { //clean up after yourself } PHP has no notion of a finally block. Anyone have experience of solutions to this rather irritating hole in the language? 回答1: Solution, no. Irritating cumbersome workaround, yes: $stored_exc = null; try { // Do stuff } catch (Exception $exc) { $stored_exc = $exc; // Handle an error } //

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