C++ 什么是*&;什么时候用在辩论中?
我想知道是什么意思。 背景: 功能的实现如下所示:C++ 什么是*&;什么时候用在辩论中?,c++,pointers,parameters,reference,arguments,C++,Pointers,Parameters,Reference,Arguments,我想知道是什么意思。 背景: 功能的实现如下所示: void headInsert( Node*& head, int info ) { Node* temp = new Node(info); temp->link = head; head = temp; } 为什么不直接使用Node& 感谢节点*&表示“指向节点指针的引用”,而节点&表示“指向节点的引用” 为什么不直接使用Node& 因为headInsert函数需要更改头部指向的对象。节点*&表示“指
void headInsert( Node*& head, int info )
{
Node* temp = new Node(info);
temp->link = head;
head = temp;
}
为什么不直接使用Node&
感谢节点*&
表示“指向节点指针的引用”,而节点&
表示“指向节点的引用”
为什么不直接使用Node&
因为headInsert
函数需要更改头部指向的对象。节点*&
表示“指向节点指针的引用”,而节点&
表示“指向节点的引用”
为什么不直接使用Node&
因为
headInsert
函数需要更改头部指向的对象。您可能需要查看特定调用,其中引用指针显示了它们的用途:
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'
让我来评论一下你的例子,也许这会让它更清楚:
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
}
您可能希望查看特定调用,其中引用指针显示了它们的用法:
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'
让我来评论一下你的例子,也许这会让它更清楚:
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
}
你应该用你正在使用的语言标记问题。你应该用你正在使用的语言标记问题。