双指针vs通过引用指针 [英] Double pointer vs pass by reference pointer

查看:216
本文介绍了双指针vs通过引用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在理解双指针概念和应该使用的地方,我有一个疑问。我已经实验了这段代码,发现我可以使用通过引用传递的指针,而不是双指针。

 #include< iostream> 
using namespace std;
void modify_by_value(int *);
void modify_by_refrence(int *&);
int a = 4,b = 5;
void main()
{

int * ptr = NULL;
ptr =& a;
cout<< * ptr before modify by value:< * ptr<< endl
modify_by_value(ptr);
cout<< * ptr after modified by value:< * ptr<< endl
cout<< * ptr before modify by refrence:< * ptr<< endl
modify_by_refrence(ptr);
cout<< * ptr after modify by refrence:< * ptr<< endl

}
void modify_by_value(int * ptr)//此函数可以更改* ptr,但不能更改ptr(包含地址)本身;
{
ptr =& b;
}
void modify_by_refrence(int *& ptr)//此函数具有引用,因此可以修改指针;
{
ptr =& b;
}

使用双指针而不是引用有什么好处?


解决方案

为什么要使用引用指针而不是指向指针?你会得到相同的答案,如果问为什么使用指针,而不是引用任何其他类型的变量...



基本上:




  • 引用(对指针或任何其他变量)是聪明的,因为在

    后面应该总是有一个对象

  • 指针(指针或任何其他变量)是聪明的,因为它们可能 NULL (可选)



  • 等...




有很多帖子已回答此问题:



指针变量和C ++中的参考变量?




是什么意思?

While understanding the double pointer concept and where it should be used,I have a doubt.I had experimented this code and found that i could use pointer passing by reference also instead of double pointers.

#include<iostream>
using namespace std;
void modify_by_value(int* );
void modify_by_refrence(int* &);
int a=4, b=5;
void main()
{

    int *ptr = NULL;
    ptr = &a;       
    cout << "*ptr before modifying by value: " << *ptr << endl; 
    modify_by_value(ptr);
    cout << "*ptr after modifying by value: " << *ptr << endl;
    cout << "*ptr before modifying by refrence: " << *ptr << endl;
    modify_by_refrence(ptr);
    cout << "*ptr after modifying by refrence: " << *ptr << endl;

}
void modify_by_value(int* ptr)      //this function can change *ptr but not the ptr(address contained) itself;
{
    ptr = &b;                       
}
void modify_by_refrence(int * &ptr) //this function has refrence and hence can modify the pointer;
{
    ptr = &b;
}

What's the benefit of using double pointers instead of reference?And where this thing should be used

解决方案

"Why using reference to pointer instead of pointer to pointer"? You will get the same answer as if asking "why using pointer instead of reference" for any other kind of variable...

Basically:

  • references (to pointer or any other variable) are smart because there should always be an object behind

  • pointers (to pointer or any other variable) are smart because they could possibly be NULL (optional)

  • references (to pointer or any other variable) are not available in C

  • references (to pointer or any other variable) are smart because they can be used as objects (no need to dereference like pointers, easier syntax, rading)

  • etc...

There are many posts answering this question already:

What are the differences between a pointer variable and a reference variable in C++?

Are there benefits of passing by pointer over passing by reference in C++?

这篇关于双指针vs通过引用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆