'const&' 在 C++ 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19415674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:47:34  来源:igfitidea点击:

What does 'const&' mean in C++?

c++operatorsconst

提问by Fela Pano

Apologies for this, I am a student trying to learn C++ and I just thought it'd be better if I ask this question and gets many correct views on this so I am sorry for asking silly questions. I just figured it's better to ask than not know.

对此表示歉意,我是一名试图学习 C++ 的学生,我只是认为如果我提出这个问题并对此有很多正确的看法会更好,所以我很抱歉提出愚蠢的问题。我只是觉得问总比不知道好。

I understand what the address operator &and the constkeyword mean separately. But when I was reading somewhere sample code I saw const&used and just wanted to what this implied?

我理解地址运算符&const关键字分别是什么意思。但是当我在某处阅读示例代码时,我看到const&使用过并且只是想知道这意味着什么?

It was used in place of an argument in a function prototype, for example:

它被用来代替函数原型中的参数,例如:

void function (type_Name const&);

If only one of them were used I would understand but I am a little confused here so some professional insight would be great.

如果只使用其中一个,我会理解,但我在这里有点困惑,所以一些专业的见解会很棒。

回答by Dietmar Kühl

Types in C++ are read right to left. This is, inconveniently just the opposite direction we like to read the individual words. However, when trying to decide what qualifiers like constor volatileapply to, putting the qualify always to the right make it easier to read types. For example:

C++ 中的类型从右到左读取。这很不方便,正好与我们喜欢阅读单个单词的方向相反。然而,当试图决定什么限定符喜欢constvolatile适用于什么时,将限定符始终放在正确的位置可以更容易阅读类型。例如:

  • int* constis a constpointer to a [non-const] int
  • int const*is a [non-const] pointer to a const int
  • int const* constis a constpointer to a const int
  • int* constconst指向 [非const]的指针int
  • int const*是一个 [非const] 指针,指向一个const int
  • int const* const是一个const指向a的指针const int

For whatever unfortunate accident in history, however, it was found reasonable to also allow the top-level constto be written on the left, i.e., const intand int constare absolutely equivalent. Of course, despite the obvious correct placement of conston the right of a type, making it easy to read the type as well as having a consistent placement, many people feel that the top-level constshould be written on the left.

不管出于什么不幸的事故历史,然而,人们发现合理的中,也允许顶层const被写在左边,也就是const intint const绝对等同。当然,尽管明显正确放置constconst字体右侧,便于阅读字体并且放置一致,但许多人认为顶层应该写在左侧。

回答by LihO

In this context, &is not an address-of operator.

在这种情况下,&不是地址运算符。

void function (type_Name const&);

is equivalent to:

相当于:

void function (const type_Name &);

which is nothing but prototype of function taking argument by constreference. You should study this kind of topics on your own, ideally from some good book. However, if you're looking for some additional material, these questions might help you too:
What are the differences between a pointer variable and a reference variable in C++?
Pointer vs. Reference, When to use references vs. pointers, Pass by Reference / Value in C++

这只不过是通过const引用获取参数的函数原型。你应该自己研究这类主题,最好是从一些好书中。但是,如果您正在寻找一些额外的材料,这些问题也可能对您有所帮助:
C++ 中的指针变量和引用变量之间有什么区别?
指针与引用何时使用引用与指针在 C++ 中通过引用/值传递