Reputation: 63
I was wondering was *& means. Context:
A function is implemented as follows:
void headInsert( Node*& head, int info )
{
Node* temp = new Node(info);
temp->link = head;
head = temp;
}
WHy not just use Node& ?
Thanks
Upvotes: 0
Views: 84
Reputation: 586
You may want to look at the specific call, where reference pointers reveal their use:
Node* pHead = somewhere;
headInsert(pHead, info);
// pHead does now point to the newly allocated node, generated inside headInser,
// by new Node(info), but NOT to 'somewhere'
Let me comment on your example, maybe that makes it more clear:
void headInsert( Node*& head, int info )
{
Node* temp = new Node(info); // generate a new head, the future head
temp->link = head; // let the former head be a member/child of the new head
head = temp; // 'overwrite' the former head pointer outside of the call by the new head
}
Upvotes: 1
Reputation: 1819
Node*&
means "a reference to a pointer to Node" while Node&
means "a reference to Node".
Why not just use Node& ?
Because the headInsert
function needs to change what the head is pointing to.
Upvotes: 3