C ++中的指针变量和引用变量之间有什么区别? [英] What are the differences between a pointer variable and a reference variable in C++?

查看:197
本文介绍了C ++中的指针变量和引用变量之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道引用是语法糖,所以代码更容易阅读和写。



但是有什么区别?






来自以下答案和链接的摘要:


  1. - 分配任意次数,而绑定后无法重新安装引用。

  2. 指针无法指向( NULL ),而引用始终引用一个对象。


  3. 没有引用算术(但是您可以获取引用指向的对象的地址,并且可以使用引用指向的对象的地址) & obj + 5 )。

澄清一个误解:


C ++标准非常小心,以避免编译器必须
实现引用,每个C ++编译器实现
引用作为指针。也就是说,声明如:

  int& ri = i; 

如果未完全优化 分配相同数量的存储
作为指针,并将地址
放入该存储。


因此,指针和引用都占用相同的内存量。



作为一般规则,




  • 在函数参数和返回类型中使用引用来定义有用的和自文档化的接口。

  • 使用指针实现算法和数据结构。






解决方案


  1. p>可以重新分配指针:

      int x = 5; 
    int y = 6;
    int * p;
    p =& x;
    p =& y;
    * p = 10;
    assert(x == 5);
    assert(y == 10);

    引用不能,且必须在初始化时分配:

      int x = 5; 
    int y = 6;
    int& r = x;指针在堆栈上有自己的内存地址和大小(x86上为4个字节)。 ,而引用共享相同的内存地址(与原始变量),但也占用了堆栈上的一些空间。由于引用与原始变量本身具有相同的地址,因此可以将引用视为同一变量的另一个名称。注意:指针指向的可以是堆栈或堆。同上参考。我在这个语句中的声明不是指针必须指向堆栈。指针只是一个保存内存地址的变量。这个变量在堆栈上。因为引用在堆栈上有自己的空间,并且因为地址与引用的变量相同。有关堆栈与堆的详细信息。这意味着有一个引用的真实地址,编译器不会告诉你。

      int x = 0; 
    int& r = x;
    int * p =& x;
    int * p2 =& r;
    assert(p == p2);


  2. 您可以有指向指针的指针,提供额外的间接层。而引用仅提供一级间接。

      int x = 0; 
    int y = 0;
    int * p =& x;
    int * q =& y;
    int ** pp =& p;
    pp =& q; // * pp = q
    ** pp = 4;
    assert(y == 4);
    assert(x == 0);


  3. 指针可以直接赋值为NULL,而引用不能。如果你尝试足够努力,你知道如何,你可以使引用的地址为NULL。同样,如果你努力尝试,你可以引用指针,然后该引用可以包含NULL。

      int * p = NULL; 
    int& r = NULL; < - 编译错误


  4. 指针可以迭代数组, code> ++ 转到指针指向的下一个项目,并且 + 4 转到第5个元素。


  5. 指针需要用 * 访问它指向的内存位置,而引用可以直接使用。指向类/ struct的指针使用 - > 来访问它的成员,而引用使用 p>


  6. 指针是一个保存内存地址的变量。无论如何实现引用,引用都具有与它引用的项目相同的内存地址。


  7. 引用不能填充到数组中, be(由用户@litb提及)


  8. Const引用可以绑定到临时表。指针不能(不是没有一些间接):

      const int& x = int // legal C ++ 
    int * y =& int(12); //非法取消引用临时。

    这使得 const& 参数列表等。



I know references are syntactic sugar, so code is easier to read and write.

But what are the differences?


Summary from answers and links below:

  1. A pointer can be re-assigned any number of times while a reference can not be re-seated after binding.
  2. Pointers can point nowhere (NULL), whereas reference always refer to an object.
  3. You can't take the address of a reference like you can with pointers.
  4. There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5).

To clarify a misconception:

The C++ standard is very careful to avoid dictating how a compiler must implement references, but every C++ compiler implements references as pointers. That is, a declaration such as:

int &ri = i;

if it's not optimized away entirely, allocates the same amount of storage as a pointer, and places the address of i into that storage.

So, a pointer and a reference both occupy the same amount of memory.

As a general rule,

  • Use references in function parameters and return types to define useful and self-documenting interfaces.
  • Use pointers to implement algorithms and data structures.

Interesting read:

解决方案

  1. A pointer can be re-assigned:

    int x = 5;
    int y = 6;
    int *p;
    p =  &x;
    p = &y;
    *p = 10;
    assert(x == 5);
    assert(y == 10);
    

    A reference cannot, and must be assigned at initialization:

    int x = 5;
    int y = 6;
    int &r = x;
    

  2. A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address (with the original variable) but also takes up some space on the stack. Since a reference has the same address as the original variable itself, it is safe to think of a reference as another name for the same variable. Note: What a pointer points to can be on the stack or heap. Ditto a reference. My claim in this statement is not that a pointer must point to the stack. A pointer is just a variable that holds a memory address. This variable is on the stack. Since a reference has its own space on the stack, and since the address is the same as the variable it references. More on stack vs heap. This implies that there is a real address of a reference that the compiler will not tell you.

    int x = 0;
    int &r = x;
    int *p = &x;
    int *p2 = &r;
    assert(p == p2);
    

  3. You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.

    int x = 0;
    int y = 0;
    int *p = &x;
    int *q = &y;
    int **pp = &p;
    pp = &q;//*pp = q
    **pp = 4;
    assert(y == 4);
    assert(x == 0);
    

  4. Pointer can be assigned NULL directly, whereas reference cannot. If you try hard enough, and you know how, you can make the address of a reference NULL. Likewise, if you try hard enough you can have a reference to a pointer, and then that reference can contain NULL.

    int *p = NULL;
    int &r = NULL; <--- compiling error
    

  5. Pointers can iterate over an array, you can use ++ to go to the next item that a pointer is pointing to, and + 4 to go to the 5th element. This is no matter what size the object is that the pointer points to.

  6. A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access it's members whereas a reference uses a ..

  7. A pointer is a variable that holds a memory address. Regardless of how a reference is implemented, a reference has the same memory address as the item it references.

  8. References cannot be stuffed into an array, whereas pointers can be (Mentioned by user @litb)

  9. Const references can be bound to temporaries. Pointers cannot (not without some indirection):

    const int &x = int(12); //legal C++
    int *y = &int(12); //illegal to dereference a temporary.
    

    This makes const& safer for use in argument lists and so forth.

这篇关于C ++中的指针变量和引用变量之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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