Class containing itself
#1
Posted 20 March 2012 - 05:58 PM
I would like to know if a class can have an array of objects from the same class. For example if I have a class Human, can it holds an array of its children (they are humans too) ?
C++ equivalent:
class Human{
private :
list<Human> children;
}
I've tried it and it gives me the "Private data control of this class uses an illegal value for its default data ..." error. Is there a way around that or do I have to use inheritance or the composite pattern?
Thanks
#2
Posted 20 March 2012 - 06:08 PM
#3
Posted 20 March 2012 - 06:25 PM
Hi,
I would like to know if a class can have an array of objects from the same class. For example if I have a class Human, can it holds an array of its children (they are humans too) ?
C++ equivalent:
class Human{
private :
list<Human> children;
}
I've tried it and it gives me the "Private data control of this class uses an illegal value for its default data ..." error. Is there a way around that or do I have to use inheritance or the composite pattern?
Thanks
It may also be handy to state why you are trying to accomplish the above (use case), as someone maybe able to offer an alternative solution to your problem.
#4
Posted 20 March 2012 - 06:32 PM
#5
Posted 20 March 2012 - 06:46 PM
To represent a tree. I was trying to keep thing simple with the fact that the object keep a list of its own children.
This may be of interest? https://decibel.ni.c.../docs/DOC-12668
#6
Posted 20 March 2012 - 07:27 PM
#7
#8
Posted 21 March 2012 - 12:21 AM
It becomes unstable around 1000 elements because it relies upon recursion. The DVR solution is much more complex and has the weirdness involved with references, but can go much deeper.
Note that a list of 1000 elements is pretty poor... but a tree of depth 1000 is 2^1000 elements, which should be more than enough. :-)
#9
Posted 21 March 2012 - 02:13 PM
#10
Posted 21 March 2012 - 04:57 PM
#11
Posted 21 March 2012 - 05:36 PM
Not to mention lookup on variant attributes is extremely fast thanks to some optimizations in the late 8.x (?) era.Try using a Variant to represent a tree. Make the attributes of the variant also be variants. And so on. You can model a tree of any shape/dimension this way.
#12
Posted 21 March 2012 - 06:27 PM
#13
Posted 21 March 2012 - 09:44 PM
#14
Posted 21 March 2012 - 10:06 PM
Yeah, when I made that it was entirely a workaround. I experimented with a few solutions and that was the one I liked best. Here is a really basic project that shows how to do what you want and it uses properties to make it look less janky.Yeah that's what I was looking for. I just don't understand what is the type of the children in the TreeNode class.
#15
Posted 21 March 2012 - 11:10 PM
You need a DVR tree when you need to do bidirectional operations. That is pretty common when you are modeling a real-world system like Marie is. Think of a family tree. Family Tree class contains all the family members. Essentially you traverse the Family tree until you find the family member you are interested. Then you can query who that member's parents, uncles, children are very quickly. This rocks if you need efficent code on a system that you understand well.
I understand the data structures, I've done the standard data structures in Java and C. What I don't understand is why you need the DVR. In a linked list you'd have a "previous" and "next" object, in a tree it's a bunch of "child" objects and a "parent" object. What is the advantage to using a DVR instead of embedding the objects directly? I implemented (but don't have the code for right now) a linked list this way and it worked fine, although I found that the only way I could get back to the front of the list was to have a "rewind" method that looped until it reached the start; does the DVR somehow solve this problem?
#16
Posted 22 March 2012 - 01:43 PM
Finally I've redesign everything. What I haven't told is that my tree was supposed to represent rows in a postgres table. Each row has a foreign key pointing to the same table to represent its father (except for root). At first I wanted to load the whole tree, make the change on it and applied it to the database so if you wanted to cancel something in the process, the change would not have been made in the DB. Instead I decided to use the database properties like a transaction and delete on cascade. If the user decide to cancel everything, I rollback and no changes are made in the DB and if he decide to save, the commit is made. Also easy when you want to delete a subtree, you just have to delete its root and on cascade, everything pointing to it will be deleted. No recursive functions except for loading the tree. Eventually, if i have time, I could implement the Command pattern to have a undo/redo, but at the moment it's just a nice to have.
Now I see the use of my software conception class and that all the time spent on my class diagram is not lost.
#17
Posted 26 March 2012 - 03:04 PM
I implemented (but don't have the code for right now) a linked list this way and it worked fine, although I found that the only way I could get back to the front of the list was to have a "rewind" method that looped until it reached the start; does the DVR somehow solve this problem?
Could you post your node class? I am not sure how your linked list solution could move both directions if you just embed the next and previous node directly in the current nodes data. A quick picture and I'll have the 'aha' moment. The DVR is just a crutch for not having a pointer, in a problem that I believe requires one. If the problem was unidirectional (top to bottom traversing) I think dataflow is the way to go.
#18
Posted 27 March 2012 - 05:30 AM
The LinkedList class (Prev and Next are of type LinkedParent from which LinkedList inherits)
Inserting a new node into the list
Advancing through the list
"Rewinding" the list














