C++:函数获取指针或引用
C++ : function gets pointer or refernce
我不太清楚函数何时应该获取指针或引用。
假设我正在实施 BFS。这是我的实现:
// 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;
}
}
}
我的问题是:函数 BFS 应该获取指针还是引用,为什么?
另外,我很想听到更多关于实施本身的评论。
非常感谢!
使用指针作为函数参数可能有不同的方法和不同的原因
- 如果您要在 BFS 函数中进行指针运算,您应该使用指针作为参数。
- 有时检查指针是否为空并根据它执行一些操作很有用。
这似乎不是使用指针作为参数的重要原因,但null
可以在其上保存非常重要的信息。例如,有二叉搜索树实现,其中 null
指针显示节点是叶子。
在您的示例中,您还检查 root
是否为 null,在这种情况下 return。
我建议将其保留为接受指针,以便您可以检查是否为空。
Ref vs pointer benefits for C++
接受指针具有以下行为,并允许 NULL (0) 值
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
}
}
作为参考接受具有以下行为并且不允许 NULL (0) 值:
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
}
}
我不太清楚函数何时应该获取指针或引用。
假设我正在实施 BFS。这是我的实现:
// 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;
}
}
}
我的问题是:函数 BFS 应该获取指针还是引用,为什么?
另外,我很想听到更多关于实施本身的评论。
非常感谢!
使用指针作为函数参数可能有不同的方法和不同的原因
- 如果您要在 BFS 函数中进行指针运算,您应该使用指针作为参数。
- 有时检查指针是否为空并根据它执行一些操作很有用。
这似乎不是使用指针作为参数的重要原因,但null
可以在其上保存非常重要的信息。例如,有二叉搜索树实现,其中 null
指针显示节点是叶子。
在您的示例中,您还检查 root
是否为 null,在这种情况下 return。
我建议将其保留为接受指针,以便您可以检查是否为空。
Ref vs pointer benefits for C++
接受指针具有以下行为,并允许 NULL (0) 值
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
}
}
作为参考接受具有以下行为并且不允许 NULL (0) 值:
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
}
}