为什么C ++不允许重新绑定引用?

时间:2022-04-10 16:44:44

Is it a problem to rebind a reference? I've searched this question on Google, but I can't find relevant answers to this question. What made the designers of C++ decided to make it that way?

重新引用引用是一个问题吗?我在Google上搜索了这个问题,但我找不到这个问题的相关答案。是什么让C ++的设计师决定这样做?

5 个解决方案

#1


10  

Stroustrup's The Design & Evolution of C++ answers most questions of this kind. In this case, see the section §3.7 References:

Stroustrup的C ++设计与演变解决了大多数此类问题。在这种情况下,请参见§3.7参考资料部分:

I had in the past been bitten by Algol68 references where r1=r2 can either assign through r1 to the object referred to or assign a new reference value to r1 (re-binding r1) depending on the type of r2. I wanted to avoid such problems in C++.
If you want to do more complicated pointer manipulation in C++, you can use pointers.

我过去曾被Algol68引用所咬,其中r1 = r2可以通过r1分配给所引用的对象,也可以根据r2的类型为r1(重新绑定r1)分配一个新的引用值。我想避免在C ++中出现这样的问题。如果你想在C ++中做更复杂的指针操作,你可以使用指针。

#2


3  

Bjarne Stroustrup introduced references into the language to support reference parameters ("call by reference") for operator overloading. You simply don't need to rebind reference parameters.

Bjarne Stroustrup在语言中引入了对语句的引用,以支持运算符重载的引用参数(“通过引用调用”)。您根本不需要重新绑定参考参数。

If you want "rebindable references", use pointers.

如果您想要“可重新绑定的引用”,请使用指针。

#3


1  

In C++, a reference is just another name for an object. It is not an indirection; that's why you cannot make it point to a different object.

在C ++中,引用只是对象的另一个名称。它不是间接的;这就是为什么你不能指出一个不同的对象。

#4


0  

Above all, "references" are actually const pointers. If you want to "rebinding", just use normal pointers.

最重要的是,“引用”实际上是const指针。如果你想“重新绑定”,只需使用普通指针即可。

Second, we cannot rebind references within our C++.

其次,我们不能在C ++中重新引用引用。

ref1 = ref2; // It's not mean "rebinding" - it just modify the object which ref1 points.

So, we need to make a new operator, like this

所以,我们需要创建一个新的运算符,就像这样

ref1 :=: ref2;

It would be a dirty point of C++, wouldn't it?

这将是C ++的一个肮脏点,不是吗?

#5


0  

One problem is the syntax. How would such an operation be written? You would have to create an entirely new operator to remove ambiguity, a pretty big investment for something that can already be done with pointers.

一个问题是语法。如何编写这样的操作?你必须创建一个全新的运算符来消除歧义,这对于已经可以通过指针完成的事情来说是一项非常大的投资。

#1


10  

Stroustrup's The Design & Evolution of C++ answers most questions of this kind. In this case, see the section §3.7 References:

Stroustrup的C ++设计与演变解决了大多数此类问题。在这种情况下,请参见§3.7参考资料部分:

I had in the past been bitten by Algol68 references where r1=r2 can either assign through r1 to the object referred to or assign a new reference value to r1 (re-binding r1) depending on the type of r2. I wanted to avoid such problems in C++.
If you want to do more complicated pointer manipulation in C++, you can use pointers.

我过去曾被Algol68引用所咬,其中r1 = r2可以通过r1分配给所引用的对象,也可以根据r2的类型为r1(重新绑定r1)分配一个新的引用值。我想避免在C ++中出现这样的问题。如果你想在C ++中做更复杂的指针操作,你可以使用指针。

#2


3  

Bjarne Stroustrup introduced references into the language to support reference parameters ("call by reference") for operator overloading. You simply don't need to rebind reference parameters.

Bjarne Stroustrup在语言中引入了对语句的引用,以支持运算符重载的引用参数(“通过引用调用”)。您根本不需要重新绑定参考参数。

If you want "rebindable references", use pointers.

如果您想要“可重新绑定的引用”,请使用指针。

#3


1  

In C++, a reference is just another name for an object. It is not an indirection; that's why you cannot make it point to a different object.

在C ++中,引用只是对象的另一个名称。它不是间接的;这就是为什么你不能指出一个不同的对象。

#4


0  

Above all, "references" are actually const pointers. If you want to "rebinding", just use normal pointers.

最重要的是,“引用”实际上是const指针。如果你想“重新绑定”,只需使用普通指针即可。

Second, we cannot rebind references within our C++.

其次,我们不能在C ++中重新引用引用。

ref1 = ref2; // It's not mean "rebinding" - it just modify the object which ref1 points.

So, we need to make a new operator, like this

所以,我们需要创建一个新的运算符,就像这样

ref1 :=: ref2;

It would be a dirty point of C++, wouldn't it?

这将是C ++的一个肮脏点,不是吗?

#5


0  

One problem is the syntax. How would such an operation be written? You would have to create an entirely new operator to remove ambiguity, a pretty big investment for something that can already be done with pointers.

一个问题是语法。如何编写这样的操作?你必须创建一个全新的运算符来消除歧义,这对于已经可以通过指针完成的事情来说是一项非常大的投资。