0

There is something very unclear to me to when a function should get a pointer or a reference .

Lets say im implementing BFS . here is my implementation :

// Assuming There is a class Node :
class Node {
    public:
    int val;
    bool visited;
    list<Node*> neighbours;
};

void BFS (Node* root) {
    if(root == NULL) {
        return ;
    }

    queue<Node*> Q;
    Q.push(root);

    while(!Q.empty()){
        Node* temp = Q.front();
        Q.pop();

    for(list<Node*>::iterator it = root->neighbours.begin() ; it != root->neighbours.end() ; it++){
            if((*it)->visited == true) continue;
            Q.push((*it));
            (*it)->visited = true;
        }

        if(!Q.empty()){
            cout << temp->val << ",";
        } else {
            cout << temp->val << endl;
        }
    }
}

My question is : should the function BFS get a pointer or a reference and why ?

also , id love to hear some more comments on the implementation itslef.

Thanks a lot !

  • This code is broken at many places. Does it even compile? – Ajay May 08 '15 at 20:42
  • @Ajay Im just getting back in shape to writing c++ - i havent tried running it . could you point out those places ? – Lina Musallam May 09 '15 at 06:10
  • 1
    No I wont. Nobody here would. Please ask concrete questions after you have failed AFTER trying. You have compilers to show the errors. – Ajay May 09 '15 at 07:57

2 Answers2

1

There could be different approaches and different reasons why to use pointer as a function argument

  1. If you are going to do pointer arithmetic inside your BFS function you should use pointer as an argument.
  2. Sometimes it's useful to check if pointer is null and do some actions depending on it.

It may seem that this is not a big reason to use pointer as an argument, but null can hold very important information on it. For example there are binary search tree implementations where null pointer shows that the node is a leaf.

In your example also you check if root is null and return in that case.

Ashot Khachatryan
  • 2,156
  • 2
  • 14
  • 30
0

I would suggest keeping it as accepting a pointer so that you can check for null.

Ref vs pointer benefits for C++

Accepting a pointer has the following behavior, and allows for NULL (0) value

int main() {
  ...
  { 
    Graph g;
    ...
    Node x(...);   //x is a reference to a Node on the stack
    g.BFS(&x);     //Notice the need to use '&' to convert to pointer
  }

  {
    Graph g;
    ...
    Node* x = Node(...);   //x is a ("Node") pointer to a Node on the stack
    g.BFS(x);              //passes as a pointer
  }

  { 
    Graph g;
    ...
    Node* x = NULL;
    g.BFS(x)          //works -- is allowed
  }

  {
    Graph g;
    ...
    Node* x = new Node(...);   //x is a ("Node") pointer to a Node on the heap
    g.BFS(x);                  //passes as a pointer
  }
}

Accepting as a reference has the following behavior and doesn't allow for NULL (0) value:

int main() {
  ...
  { 
    Graph g;
    ...
    Node x(...);   //x is a reference to a Node on the stack
    g.BFS(x);     //Pass by reference
  }

  {
    Graph g;
    ...
    Node* x = Node(...);   //x is a ("Node") pointer to a Node on the stack
    g.BFS(*x);              //Notice the need to dereference x to pass by reference
  }

  {
    Graph g;
    ...
    Node* x = new Node(...);   //x is a ("Node") pointer to a Node on the heap
    g.BFS(*x);                  //Notice the need to dereference x to pass by reference
  }

  { 
    Graph g;
    ...
    Node* x = NULL;
    g.BFS(x)   //does not work, can't pass 0 val to function expecting a reference      
  }
}
Community
  • 1
  • 1
ZachSand
  • 143
  • 8
  • No problem! Make sure to mark your "accepted answer" (whichever it may be) for future viewers. – ZachSand May 08 '15 at 20:37
  • `Graph g();` declares a function named `g` which would return `Graph`. It doesn't declare a local variable `g` – Ajay May 08 '15 at 20:39
  • Thanks for pointing that out, not sure why I added parentheses originally. I've edited them out. – ZachSand May 08 '15 at 20:44