'why doesn't C++ allow rebinding a reference?
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?
Solution 1:[1]
Stroustrup's The Design & Evolution of C++ answers most questions of this kind. In this case, see the section ยง3.7 References:
I had in the past been bitten by Algol68 references where
r1=r2
can either assign throughr1
to the object referred to or assign a new reference value tor1
(re-bindingr1
) depending on the type ofr2
. I wanted to avoid such problems in C++.
If you want to do more complicated pointer manipulation in C++, you can use pointers.
Solution 2:[2]
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.
If you want "rebindable references", use pointers.
Solution 3:[3]
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.
Solution 4:[4]
Above all, "references" are actually const pointers. If you want to "rebinding", just use normal pointers.
Second, we cannot rebind references within our 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?
Solution 5:[5]
References are useful because they don't support unsafe pointer arithmetic and will never be null. On the other hand, pointers can be rebound and can be placed in STL containers. A trade off with all these useful properties is std::reference_wrapper:
#include <functional>
#include <iostream>
#include <string>
int main() {
std::string s1 = "my", s2 = "strings";
auto r = std::ref(s1); // bind
// use r.get() to access the referenced object
std::cout << '\'' << r.get() << "' has " << r.get().size() << " characters\n";
r = s2; // rebind
// use the other object
std::cout << '\'' << r.get() << "' has " << r.get().size() << " characters\n";
}
Solution 6:[6]
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.
Solution 7:[7]
Internally reference
is nothing but a const pointer
(once an address is assigned to that pointer then you can change it in a straightforward way).
int& r = a; // int* const r = &a;
The sample code below explains what will happen behind the screen.
int main() {
int a = 10;
int b = 20;
int& r = a; // = [int* const r = &a;]
r = b; // = [*r = b] value update. NOT address update [r = &b]
++r; // = [++*r]
cout << r << a << b; // 21 21 20 (bcos r is NOT rebinded to b)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | fredoverflow |
Solution 3 | axiac |
Solution 4 | ikh |
Solution 5 | Ayxan Haqverdili |
Solution 6 | glank |
Solution 7 | SridharKritha |