Pieman wrote:
Quote:
From a didactic point of view, variables created like in your main(), should not be delete-ed, the example is thus confusing in that respect
(use 'new' in main() to dynamically create the variables).
I know... doesn't
#ifdef DynamicDestructors imply that they aren't?
Sort-of, but if you have to compile without, what's the point of adding the delete code in the first place?
Many of the comments are about how wonderful the recursive delete is, except the code does not actually use it.
Tbh, in a real project I would consider such conditional compilation code way too dangerous to ever include. I consider it too likely that
-DDynamicDestructors is added or removed by accident, the compiler will not complain, and during execution it fails horribly in all kinds of interesting ways. It takes several days before you find this problem.
Usually, I design a data structure for exactly one purpose. If I need two cases, I either make it completely separate, or use inheritance to create the variation.
Pieman wrote:
Also destructors are automatically called when you leave a scope and the instances are not dynamic, but rather when just introduced like: Type instance; (instead of Type* instance = new Type) -- otherwise you must use the delete operator, which I don't. I don't really understand your problem here. Would you prefer I add a second #ifdef Dynamic in the main loop, where one uses dynamic memory, and the other doesn't?
What do you actually want to demonstrate here?
If you're aiming for a demonstration of a tree library, making the code more generally usable is a good idea, provided it has some text explaining how to use it.
If you're aiming for an example of recursive delete, I'd drop everything that distracts from the core idea.
The difference (for me, that is) between the two is that a 'tree library' would be used with copy/paste in one's project, while an example is only for making the idea clear, the code itself is not aimed at copy/paste use.
Pieman wrote:
Quote:
For clarification of the structure, a picture would be very helpful.
Ok! :)
Unfortunately I didn't provide any description with this topic in the first place, sorry about that. I might create a diagram when I'm ready. I wanted more input on the code actually.
I have seen too many tree-structures to give useful input probably; I use and manipulate recursive structures just as easily as integers.
The only thing I was wondering about is what type of data you are storing in the tree, ie what is the 'testProperty' about? Perhaps you can use a somewhat more interesting problem to model. The first thing that comes to mind is a decision tree; the parent states which variable to use, and the siblings are value intervals of that variable.
Pieman wrote:
Well, for practical purposes do you think it can be improved, and how? If you have no idea, then that's okay too. :)
Probably, but the direction is not clear as I don't understand what you're trying to explain.
If you're looking for a use-case for tree-like structures, perhaps a scanning or parsing problem would be fun, ie design your own input format for (game) data, or even code-ish (eg read a decision-tree or expression from file to be used in the game).
Quote:
I hope you clarify your point about the delete operator.
I hope so too. If not, please explain what part is not clear or missing.
_________________
My project: Messing about in
FreeRCT,
dev blog, and IRC #freerct at oftc.net