Pointers to Classes: Back and Forward

Sep 1, 2012 at 7:07am
I'm beginning to understand Classes and I thought I would implement somethig
usefull for my studies, a graph. Basically, a graph is a set of nodes (points)
with a (variable) number of links (sticks) going (pointing) to other nodes.

The part "set of nodes" is kind of simple... but the "node" part is where it gets tricky (for me at least).

To implement this I tried :

1
2
3
4
5
6
7
8
 class node{
    protected:
      link **linkPointer;  // a dynamic array of links (or so I think)
};

class link{
     node* to;  // pointer to a node Class
};


I think I understand why this fails. I'm declaring something circular, meaning link needs node, and node needs link.

I thought this was quite simple... turns out it is not THAT simple.
Any ideas how can I implement this??
Sep 1, 2012 at 7:17am
While this can be done quite easily, it doesn't make a lot of sense for edges to have their own class. Edges are merely the things that connect two nodes, and C++ already has something like that: pointers.
1
2
3
4
class node{
protected:
      node **linkPointer;
};
Sep 1, 2012 at 8:30am
Well... yes, pointers can do that, but the thing is that (eventually) these links will have LOTS of properties like how "strong" the bond is, how "far" does it go... they may enven have their own functions .... that is why I put the links in a separate class.... so that they can the can GROW.

Just didn't want to bother everyone with the details of further implementations. I tried to get the essence of a graph.
Last edited on Sep 1, 2012 at 8:32am
Sep 1, 2012 at 8:35am
Alright, then.
Just forward declare link.
1
2
3
4
class link;

class node{
//... 
Sep 1, 2012 at 12:34pm
That worked beautifully.
Topic archived. No new replies allowed.