Stuck...
2007-02-09 20:02:28.24102+00 by
ebradway
4 comments
I think I coded myself into a box. I created this great hierarchy of classes to mimic my SQL database. It looks something like this:
Forest->Grove->Tree->Strip->ConvexHull
With each step being a one-to-many relationship except for strips and convexhulls, which are one-to-one. My actual vertices live in an array that's part of "grove". Everything below grove consists of subsets of the array of vertices and each grove has it's own array of vertices.
My problem is that I'm way down in ConvexHull and want to access my array of vertices. Is there a way in C++ to say "I want a pointer to the 'parent' Grove of this class?"
This gets really confusing because I'm talking about 'parent' in terms of a hierachy of classes but not the typical OOP meaning.
I know two brute-force ways to do it. One, I could make my Forest a global but that'd really suck. Two, I could pass the pointer to Grove down through Trees and Strips to ConvexHull. But there's got to be a more elegant way...
[ related topics:
Software Engineering Databases C++
]
comments in ascending chronological order (reverse):
#Comment Re: made: 2007-02-09 21:33:18.86589+00 by:
Dan Lyke
[edit history]
Wait, so a forest owns many groves, which owns many trees, which owns many strips, which owns a convex hull?
Or a convex hull is a subclass of a strip which is a subclass of a tree which is a... you get the picture.
I think you mean the first case, in which case I think you want something outside the hierarchy which is your vertices, and grove knows about that collection (presumably because it associates them with trees?), but so does convex hull.
When you instantiate ConvexHull, you have to do so with a list of points around which it's going to calculate, presumably.
Added in edit:
Depending on how you're modeling the data, the list of points might be entirely separate, or might be associated with the trees, at which point a convex hull surrounds a collection of trees, and a grove happens to be a collection of trees, but you might also have intersecting groves or...
Yeah, how about some explanation about how you came to that particular model of the data?
#Comment Re: made: 2007-02-09 22:06:31.633957+00 by:
ebradway
[edit history]
I really didn't want to get too much into the model. I just wanted to get to a element in the parent object in the hierarchy. And yeah, it's not a object hiearchy in terms of subclasses and inheritnace and such. Quite literally, I can have a pointer that looks like:
pForest->pGrove[1]->pTree[1]->pRoot->pConvexHull->paVertices
This would give me a pointer to a list of array indices into an array here:
pForest->pGrove[1]->paVertices;
What I wanted as an easy way to reference pForest->pGrove[1]->aVertices from a member function in ConvexHull without either making my Forest global or passing a pointer to the array all the way down to ConvexHull.
But alas, I went ahead and changed the code to pass the pointer to the array all the way down.
#Comment Re: made: 2007-02-10 02:13:58.5898+00 by:
Jack William Bell
What you have is a very common issue with object-container hierarchies in most OOPL. (Smalltalk, sort of excepted.)
Your answer works perfectly; hopefully you are passing the pointer to the constructor; which is the easy way to do it. If you disallow the default constructor, by making it private, you can even force calling code to use the proper constructor.
Another, somewhat more elegant, example (which is a little more code) is to copy the structures used by XML DOM implementations. The beauty of the node/container hierachies of a DOM is that the links go both ways, and are established in the base classes. So all child classes are nodes of a kind and some nodes are containers. All nodes not only know their parent, they often know their immediate siblings and the root (document) node as well. As a result it is child's play to write code that traverses a DOM looking for a specific node.
#Comment Re: made: 2007-02-12 17:43:01.345702+00 by:
ebradway
Hmmm... XML DOM... I'm not quite ready to bite off another learning curve. The jump to C++ will be enough for now. However, this data structure will ultimately be implemented as C++, Java, and SQL, so working from an abstraction layer might be final solution.