I'm having trouble understanding some C++ concepts.
When is it appropriate to use a member pointer vs a member variable?
Let's say I have a tree structure and thus a Node
class. My intuition would be to use a variable for its Data
and Edge
(because the Node
"owns" the Data
) and a pointer to the parent Node
. Is this correct?
class Node {
public:
Data data;
Edge edge;
Node *parent;
}
When I implement a custom creator method for the Node
class, I also have to initialize the member variables (but not the pointers?). For the root Node
I can set the parent pointer to NULL
.
But what do I do with the Edge
variable? The root Node
has no parent and no Edge
leading to it from its parent. Can I somehow set it to a "zero" value? If it matters, the Edge
class has no default constructor, but something like this: Edge(int length, int width)
.