Original Post
In my class hirearchy, there is a general "composite" class that can dynamically attach other child "composites" or "primitives" to itself, thus forming an implicit tree-type structure.
I want to be able to attach both stack-allocated and heap-allocated (e.g. both class/local variables and dynamic variables) to these composites. The problem I run into, though, is when attaching unnamed, dynamic objects. They must be destroyed eventually. The problem is when and by whom.
Here are my solutions:
1) I could track all dynamic objects externally to the composite class, but that would be extra overhead, being that the composite class already tracks it. Deciding when to destroy them would be a similarly silly, duplicated task.
2) I could include a boolean flag in the attach function to state whether or not the attaching object should be dynamically destroyed when the composite object is destroyed, but that''s too much special-case work for this hirearchy, I feel.
3) I could just try to destroy all of the attached objects when the composite object is destroyed, catching all exceptions raised from trying to delete a non-dynamic object.
Though I feel 2 is the most correct, I''ve decided to go with 3 because it has the least global overhead. I feel that it''s bad programming practice, though, and it abuses the try-catch paradigm much in the same way that most people complain about.
Any comments or suggestions?
Thank you for your bandwidth.
-- Succinct
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~