10

I've been reading through an implementation of a List (and its node) using classes and I've found a couple things I don't quite understand. Here's the code in the Node class that I don't understand:

    class Node {
      private:
         Data data;
         Node* next;
      public:
         Node*& getNext();
    };

    Node*& Node::getNext()
    {
       return this->next;
    }

What is *& exactly? I don't get what kind of variable is returned by that method.

I think I get it now, later on I have these lines (inside class List):

Node** node = &first;
node = &(*node)->getNext();       

Does that mean I'm storing next's address in node*?

Note: Second question was answered in the comments. Thanks for replies.

user1769106
  • 308
  • 3
  • 8
  • 1
    It's a reference to a pointer. – Captain Obvlious Oct 23 '12 at 17:47
  • 1
    You should always pose two questions when you think returning a reference might be a good idea: 1) Is the thing being returned actually owned by the user of `Node`? 2) Will the reference never outlive the object that it refers to? Usually only consider returning a reference if both answers are yes. – Joseph Mansfield Oct 23 '12 at 17:55

4 Answers4

9

That's a reference to a pointer. It means that the Node* the function returns is an alias for Node::next.

For example, say you have:

Node n;
n.getNext() = NULL;

this sets n.next to NULL.

If the method didn't return by reference

Node* Node::getNext()  //no reference
{
   return this->next;
}

the same code

Node n;
n.getNext() = NULL;

wouldn't modify n.next - and, in this case, it would remain un-initialized compile because getNext returns an rvalue here.

Alternatively, returning by reference:

Node*& x = n.getNext();
x = new Node;

would modify n.next, as x is a reference to n.next.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • -1 `n.getNext() = NULL;` for `Node* Node::getNext()` would not compile. you can't assign to an rvalue. – Cheers and hth. - Alf Oct 23 '12 at 18:05
  • I think I get it now. Later on (on class List) I have these lines `Node** node = &first` and `node = &(*node)->getNext();` does that mean I'm storing next's address in node*? – user1769106 Oct 23 '12 at 18:54
4
Node*& getNext();

Returns a reference to a Node*. Why they chose to return a non-const reference to a pointer, which allows for its value to be changed by callers of the function... I don't know.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
4

That means you're returning a pointer by reference. Meaning someone can modify the actual pointer inside a node:

Node anode = /* something... */;
anode.getNext() = nullptr;
assert(anode.getNext() == nullptr); // assertion passed!

This doesn't look like a situation in which you would use this. Just return the pointer:

Node* Node::getNext()
{
   return next;
}
mfontanini
  • 21,410
  • 4
  • 65
  • 73
4

Compare

class Node {
  private:
     Data data;
     Node* next;
  public:
     Node*& getNext();
};

Node*& Node::getNext()
{
   return this->next;
}

to

class Node {
  private:
     Data data;
  public:
     Node* next;
};

These code snippets achieve almost the same, but the latter is

  • shorter,

  • simpler, and

  • allows accessing the next pointer also on a const object.

So, given all that needless complexity and limitation, not to mention Java-isms such as the Get prefix and the use of this->, you can safely assume that the constructs and even naming that you see in that code, are most probably not meaningful, and even with adverse effect – as certainly the GetNext method has, limiting the access.

Tech details: & is C++ speak for "reference", and the most basic explanation of a C++ reference is that it is an alias for some object. In C++03 a reference was indistinguishable from the object that it referred to. No matter the C++ version, there is no such thing as a null-reference in valid code.

To really learn about pointers and references you should use a good C++ textbook, and not rely on answers in net forums.

The SO C++ FAQ book list has many good suggestions for textbooks.

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331