简体   繁体   English

不确定有关C ++ Primer中的Reference的描述

[英]Unsure about a description regarding Reference in C++ Primer

The book said: Because references are not objects, we may not define a reference to a reference. 书中说:因为引用不是对象,我们可能没有定义对引用的引用。

int ival = 1024;
int &refVal = ival;
refVal = 2;
int ii = refVal;
int &refVal3 = refVal; // isn't this a definition of ref to ref?
int i = refVal;
int &refVal4 = 10;
double dval = 3.14;
int &refVal5 = dval;

However, that line is not an error, because refVal3 is saying it is just another alias for refVal, and refVal is just another name for ival(refVal3 is bound to the object to which refVal is bound to, which is ival)... so both refVal and refVal3 refer to the initializer of ival. 但是,该行不是错误,因为refVal3说它只是refVal的另一个别名,而refVal只是ival的另一个名称(refVal3绑定到refVal绑定到的对象,即ival)...所以refVal和refVal3都引用了ival的初始化器。

That makes perfect sense, but if that's not a definition of a reference to a reference, just what exactly does the book mean when it mentioned "Because references are not objects, we may not define a reference to a reference." 这是完全合理的,但如果这不是对引用的引用的定义,那么当它提到“因为引用不是对象时,我们可能没有定义对引用的引用”这本书究竟是什么意思。 ?? ??

Can someone perhaps give me an example ? 有人可以举个例子吗?

Your understanding is correct. 你的理解是正确的。

int &refVal3 = refVal;

This makes refVal3 a reference to the same thing refVal is a reference to. 这使得refVal3引用了同样的东西, refVal就是refVal的引用。 That is, ival . 也就是说, ival

just what exactly does the book mean when it mentioned "Because references are not objects, we may not define a reference to a reference." 当书中提到“因为引用不是对象时,我们可能无法定义对引用的引用”。 ?

A reference can only refer to an object. 引用只能引用一个对象。 But references are not objects. 但引用不是对象。 So a reference cannot refer to a reference. 所以引用不能引用参考。 And when the book says this, it doesn't just mean that it's not allowed, and you'll get an error if you try. 当这本书说明这一点时,它并不仅仅意味着它不被允许,如果你尝试,你会得到一个错误。 But you can't even try. 但你甚至无法尝试。 There's no syntax for it. 它没有语法。 Because anything you try to do to a reference, you will actually be doing to the object it refers to, and not to the reference itself. 因为您尝试对引用执行任何操作,您实际上将对其引用的对象执行操作,而不是对引用本身执行操作。

Can someone perhaps give me an example ? 有人可以举个例子吗?

No. I can't. 不,我不能。 And that's the point. 这就是重点。 There's simply no syntax for it. 根本就没有它的语法。

Reference-to-reference types (like T & & ) do not exist in C++. C ++中不存在引用类型(如T & & )。

Where T is an object type (which includes int , as in your example): 其中T是一个对象类型(包括int ,如在你的例子中):

(Similarly, cv-qualified types like const T& exist, while types like const T & & do not exist.) (类似地, cv限定类型const T& exists,而const T & &等类型不存在。)

You asked for an example. 你问了一个例子。 Consider this wrong code: 考虑这个错误的代码:

int main()
{
    int ival = 1024;
    int &refVal = ival;
    int & &refRefVal = refVal;   // wrong
}

This is an error because there is no such type as int & & . 这是一个错误,因为没有int & &这样的类型。 It would be an error regardless of what I tried to initialize it with. 无论我试图用什么来初始化它都是一个错误。

(Strictly speaking, it is an error because the syntax of the language prohibits it. The standards committee could have chosen to allow me to write int & & and have it mean the same thing as int & --see Reference Collapsing below--but they didn't, which is good, because that would be very confusing.) (严格来说,这是一个错误,因为语言的语法禁止它。标准委员会可以选择允许我写int & &并且它意味着与int &相同的东西 - 参见下面的参考折叠 - 但是他们没有,这很好,因为那会很混乱。)

When I attempt to compile that wrong code with Clang 3.8, I get: 当我尝试使用Clang 3.8 编译错误的代码时,我得到:

error: 'refRefVal' declared as a reference to a reference 错误:'refRefVal'被声明为对引用的引用

Other compilers give similar errors. 其他编译器也会出现类似错误。 For example, Microsoft Visual C++ gives : 例如, Microsoft Visual C ++提供

error C2529: 'refRefVal': reference to reference is illegal 错误C2529:'refRefVal':对引用的引用是非法的

When you use a reference, the effect is to use the object it refers to. 使用引用时,效果是使用它引用的对象。

References are dereferenced automatically in most contexts where they appear. 引用在它们出现的大多数上下文中自动解除引用。 Anything you try to do to a reference, really you are doing it to the object it refers to. 你尝试对引用做的任何事情,实际上你正在对它引用的对象做这件事。 Unlike pointers, there is no operator for dereferencing a reference; 与指针不同,没有用于取消引用引用的运算符; in effect the reference is a name for the referenced object. 实际上,引用是引用对象的名称。

What you have written ( int &refVal3 = refVal; ) is not an error, because you are simply initializing a second reference bound to the same object. 您编写的内容( int &refVal3 = refVal; )不是错误,因为您只是初始化绑定到同一对象的第二个引用。 To see why this is, consider the effect of several of your statements. 要了解其原因,请考虑几个语句的效果。

Here you create an int , initializing it with the value 1024 : 在这里创建一个int ,用值1024初始化它:

int ival = 1024;

Here you make an lvalue reference, bound to that int object: 在这里,您创建一个左值引用,绑定到该int对象:

int &refVal = ival;

Here you assign 2 to the original int object, because refVal is used as the object to which it refers: 在这里,您将2指定给原始int对象,因为refVal用作它所引用的对象:

refVal = 2;

Here you create a second int object, initialized with the value of the original object, also because refVal is used as the object to which it refers: 在这里,您创建第二个int对象,使用原始对象的值初始化,也因为refVal用作它引用的对象:

int ii = refVal;

Here you make a second lvalue reference to the original object, also because refVal is used as the object to which it refers: 在这里,您对原始对象进行第二次左值引用,也因为refVal用作它所引用的对象:

int &refVal3 = refVal;

Code that looks like it creates a second reference to the first one is, therefore, really creating a second reference to the original object. 因此,看起来像它创建对第一个引用的第二个引用的代码实际上是创建对原始对象的第二个引用。

This is to say that the reason int &refVal3 = refVal; 这就是int &refVal3 = refVal;的原因int &refVal3 = refVal; introduces another reference to the original object--rather than attempting to create a reference to a reference--is that this is just another consequence of refVal being automatically taken to mean the int it refers to . 引入了对原始对象的另一个引用 - 而不是试图创建对引用的引用 - 这只是refVal被自动refVal为它引用的int另一个结果

Reference Collapsing 参考折叠

You can't write types named like T & & yourself, but what about this? 你不能写出像T & &你自己这样的类型,但是这个呢?

using Ref = int&;
using RefRef = Ref&; // I named this poorly, it's not really a reference to a reference!

This causes the compiler to see that I am trying to make a type alias RefRef to be int& & . 这会导致编译器看到我正在尝试将类型别名RefRef设置为int& & The compiler follows the rules of reference collapsing . 编译器遵循参考折叠规则 It collapses the two references into one, so the effect is the same as if I had written: 它将两个引用合并为一个,因此效果与我写的一样:

using RefRef = int&;

This behavior is useful in situations that involve type deduction, such as with templates , both by allowing more code to compile and work as expected than otherwise would, and by facilitating perfect forwarding . 这种行为在涉及类型推导的情况下非常有用,例如使用模板 ,通过允许更多代码按预期编译和工作,以及促进完美转发 (One might argue it also parallels what you observed--when you initialize references from references, you can still never get a reference to a reference, only to an object.) (有人可能会认为这也是平行你观察到的东西-当你初始化从参考参考,你仍然可以永远不了的参考的参考,只有一个对象。)

In no case is there ever anything whose type is reference to reference. 在任何情况下都没有任何类型参考参考的东西。 The C++ language simply does not have any such types. C ++语言根本就没有这样的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM