加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 通过引用调用和通过指针调用之间的差异

发布时间:2020-12-16 03:46:06 所属栏目:百科 来源:网络整理
导读:这个问题在这里已经有一个答案: What are the differences between a pointer variable and a reference variable in C++?30个 任何人都可以告诉我通过指针调用和通过引用调用之间的确切差异.实际上这两种情况发生了什么? 例如: 通过参考调用: void swap
这个问题在这里已经有一个答案:> What are the differences between a pointer variable and a reference variable in C++?30个
任何人都可以告诉我通过指针调用和通过引用调用之间的确切差异.实际上这两种情况发生了什么?

例如:

通过参考调用:

void swap(int &x,int &y)
{
   int temp;
   temp = x; /* save the value at address x */
   x = y;    /* put y into x */
   y = temp; /* put x into y */

   return;
}

swap(a,b);

通过指针拨号:

void swap(int *x,int *y)
{
   int temp;
   temp = *x; /* save the value at address x */
   *x = *y; /* put y into x */
   *y = temp; /* put x into y */

   return;
}

  swap(&a,&b);

解决方法

这两种情况都完全一样.

但是,小的区别是,引用永远不会为空(并且内部函数确实是引用有效变量).另一方面,指针可能为空,或者可能指向存储器中的无效位置,导致AV.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读