1

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).

Jawap
  • 2,463
  • 3
  • 28
  • 46
  • What does the `Edge` represent? How is it defined? – Fred Foo Nov 22 '12 at 17:16
  • 1
    @Jawap Why do you not add a constructor `Edge()` in `Edge` class to initialise a edge to zero. constructor overload! – Grijesh Chauhan Nov 22 '12 at 17:21
  • @larsmans The edge between the node and its parent node. It stores properties about the edge such as length, name. – Jawap Nov 22 '12 at 17:23
  • @GrijeshChauhan How do I initialize it to zero? Do I have to add a member variable isZero and set it to true? – Jawap Nov 22 '12 at 17:25
  • @Jawap : Add one more constructor in your `Edge` class. `Edge(){ this->length=0; this->width=0; }` But better solution given by @sampson-chen. And the concept called default initialisation of function parameter. – Grijesh Chauhan Nov 22 '12 at 17:33
  • Caution! I above comment I am assuming `length` and `width` are data members of `Edge class`. – Grijesh Chauhan Nov 22 '12 at 17:35
  • Also according to OOP concepts if there required many changes(extensions) in `Edge` class then better it to create a subclass of `Edge` that is called `inheritance`. – Grijesh Chauhan Nov 22 '12 at 17:40

2 Answers2

2

For appropriate use of member pointer vs. member variable, see this discussion.

Now, here is what you can do with the Edge class with regard to the "zero" value:

Change the constructor of Edge to read:

Edge(int length=0, int width=0)

This way when Edge is default constructed with Edge e1 (or implicitly with Node n1), that constructor will be matched and the default values as specified will be used.

I'd say an edge of length 0 and width 0 makes perfect sense as a "zero" edge.

I've only listed one way to define default values for the constructor. There is additional discussion on the topic here.

Community
  • 1
  • 1
sampson-chen
  • 45,805
  • 12
  • 84
  • 81
1

When the "custom creator" method (correctly termed the "constructor") for the Node class runs, any members of Node which are themselves objects (i.e. not basic types like ints, floats, chars, and not pointers) will have their own constructors run.

So the initial value of the edge object will be determined by what the constructor of the Edge class does. If the Edge class constructor requires arguments, then the Node constructor will have to provide them.

Suppose, for example, that the Edge constructor requires an integer called weight, e.g.:

class Edge {
  public:
    Edge(int w)
      : weight(w) {
    }

  private:
    int weight;
};

Then there are two options for giving a default weight to this edge. You can give the edge constructor have a default value for this parameter:

class Edge {
  public:
    Edge(int w = 0)
      : weight(w) {
    }

  private:
    int weight;
};

In which case the Node class does not need to do anything special. Or, you can have the Node class provide the parameter, e.g.:

class Node {
  public:
    Node()
      : edge(0),
        parent(NULL) {
    }

    Data data;
    Edge edge;
    Node *parent;
};
Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166