在Go和C++中,指针和引用之间的逻辑差异是什么?

huangapple go评论92阅读模式
英文:

logical difference between pointers and references in Go and C++?

问题

无论是指针还是引用,都有其自身的优势和规则。指针可以直接操作内存地址,可以进行空指针的赋值,但需要进行解引用操作。而引用则不需要解引用,更加方便,但在某些语言中有一些特定的规则,比如在C++中引用不允许赋值为NULL。因此,提供指针和引用这两种语言构造是有其逻辑原因的,而不仅仅是一种语法糖。至于编译器的底层实现,可能会对引用和指针采取相同的步骤,但会根据语言定义的规则进行隐含或检查。

英文:

>Regardless of provided convenience of references over pointers such as needleless of dereferencing and rules specific to use of each ,
>
>is there any logical reason to provide two language constructs as pointers and references or is it just a syntactic sugar?

(I guess the ultimate underlying implementation that compiler would use the same steps for references as do for pointers with implying/checking for rules defined for references by a language.)

<i>NOTE : the question is not about rules have defined by languages on references such as "references are not allowed to assign NULL in C++ but pointers" etc.</i>

答案1

得分: 3

你正在提出两个问题,如果我理解正确的话

  1. 指针和引用之间有什么区别
  2. 为什么支持这两种数据类型

下面是答案:

  1. 指针是指向内存中数据类型所在位置的。指针的大小是固定的,根据底层硬件确定,通常是4或8个字节,与指针实际指向的内容无关。此外,指针可以使用无效值传递给函数 - foo(reintepret_cast&lt;int *&gt;(0xDEADBEEF) );
    相比之下,引用确保底层数据是有效的 - 因为引用是对象本身的别名,不能从别名中移动(前提是引用的对象仍在作用域内 - 根据下面的备注进行了编辑)。

  2. 支持这两种类型是有原因的。第一个原因是确保传递给函数的数据是有效的 - 而不浪费时间在测试指针有效性(不是NULL)上。第二个原因是可以确保数据不仅指向一个有效的位置,还指向一个有效的数据对象。但主要原因是引用允许我们享受通过引用传递参数而不是按值传递参数的好处,同时仍然保证参数引用一个有效的值。

英文:

You are asking two questions, if I understand correctly

  1. What is the difference between pointers and references
  2. Why support both data types

Here goes:

  1. A pointer refers to a location in memory where a datatype resides. The size of a pointer is fixed, given the underlying hardware, generally 4 or 8 bytes - totally regardless of what it is in fact pointing to. Furthermore, a pointer can be passed to a function using an invalid value - foo(reintepret_cast&lt;int *&gt;(0xDEADBEEF) );.
    In contrast, a reference ensures that the underlying data is valid - since the reference is an alias the the object itself and can't be moved from being so (providing the the referenced object is still in scope - edited per remark below).
  2. There are reasons to support both types. The first reason is to ensure that data that is passed to functions is valid - without wasting cycles on testing pointer validity (not NULL). The second reason is that one can be sure that not only is the data pointing to a valid location, it is also pointing at a valid data object. But the main reason is that a reference allows us to enjoy the benefit of calling a function without passing arguments by value, yet still maintaining a guarantee that the argument refers to a valid value.

答案2

得分: 0

你应该问Bjarne Stroustrup为什么C++有引用。他在《C++程序设计语言》第四版的第7.7节“引用”中提供了他的理由,第189页。还可以参考《C++的设计与演化》第86页。

英文:

You should ask Bjarne Stroustrup why C++ has references. Amongst other places, he provides his rationale in Section 7.7 References, The C++ Programming Language, Fourth Edition on page 189. See also p. 86 of The Design and Evolution of C++.

huangapple
  • 本文由 发表于 2013年7月7日 19:22:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/17511701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定