lqr
lqr

Reputation: 1067

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?

Upvotes: 18

Views: 4296

Answers (7)

SridharKritha
SridharKritha

Reputation: 9671

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)
}

Upvotes: 1

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29985

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";
}

Upvotes: 1

glank
glank

Reputation: 391

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.

Upvotes: 1

Jonathan Wakely
Jonathan Wakely

Reputation: 171373

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 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.

Upvotes: 20

ikh
ikh

Reputation: 10417

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?

Upvotes: 2

fredoverflow
fredoverflow

Reputation: 263220

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.

Upvotes: 6

axiac
axiac

Reputation: 72256

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.

Upvotes: 2

Related Questions