Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are references not reseatable in C++

C++ references have two properties:

  • They always point to the same object.
  • They can not be 0.

Pointers are the opposite:

  • They can point to different objects.
  • They can be 0.

Why is there no "non-nullable, reseatable reference or pointer" in C++? I can't think of a good reason why references shouldn't be reseatable.

Edit: The question comes up often because I usually use references when I want to make sure that an "association" (I'm avoiding the words "reference" or "pointer" here) is never invalid.

I don't think I ever thought "great that this ref always refers to the same object". If references were reseatable, one could still get the current behavior like this:

int i = 3; int& const j = i;

This is already legal C++, but meaningless.

I restate my question like this: "What was the rationale behind the 'a reference is the object' design? Why was it considered useful to have references always be the same object, instead of only when declared as const?"

Cheers, Felix

like image 261
TheFogger Avatar asked Apr 08 '09 01:04

TheFogger


People also ask

Why there is no call by reference in C?

Each programming language has its own terminology. And in C call by reference or pass by reference means pass indirectly through a pointer to an object. This wrong statement arises when somebody is saying about C but is using the terminology of the definition of the term reference introduced in other languages.

Why are reference arrays not allowed?

An array of references is illegal because a reference is not an object. According to the C++ standard, an object is a region of storage, and it is not specified if a reference needs storage (Standard §11.3. 2/4). Thus, sizeof does not return the size of a reference, but the size of the referred object.

Why is reference not same as pointer in C?

References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced but pass by reference.

Are references available in C?

No, it doesn't.


2 Answers

The reason that C++ does not allow you to rebind references is given in Stroustrup's "Design and Evolution of C++" :

It is not possible to change what a reference refers to after initialization. That is, once a C++ reference is initialized it cannot be made to refer to a different object later; it cannot be re-bound. 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++.

like image 174
Michael Burr Avatar answered Oct 22 '22 16:10

Michael Burr


In C++, it is often said that "the reference is the object". In one sense, it is true: though references are handled as pointers when the source code is compiled, the reference is intended to signify an object that is not copied when a function is called. Since references are not directly addressable (for example, references have no address, & returns the address of the object), it would not semantically make sense to reassign them. Moreover, C++ already has pointers, which handles the semantics of re-setting.

like image 27
rlbond Avatar answered Oct 22 '22 18:10

rlbond