41

As the title itself mentions - why are pointer to a reference illegal, while the reverse is legal in C++?

3
  • 1
    Looks like someone took my words seriously from here stackoverflow.com/questions/4632528/… .... that's good :D Commented Jan 8, 2011 at 6:59
  • 1
    Anyway, +1 for the good question. even I want to know the rationale! Commented Jan 8, 2011 at 7:00
  • 1
    @Nawaz: Your guess is correct. Commented Jan 8, 2011 at 7:04

5 Answers 5

53

A pointer needs to point to an object. A reference is not an object.

If you have a reference r, once it is initialized, any time you use r you are actually using the object to which the reference refers.

Because of this, you can't take the address of a reference to be able to get a pointer to it in the first place. Consider the following code:

int x;
int& rx = x;

int* px = ℞

In the last line, &rx takes the address of the object referred to by rx, so it's exactly the same as if you had said &x.

Sign up to request clarification or add additional context in comments.

3 Comments

This is the most relevant answer so far. Since it is impossible to take the address of a reference, what would be the point of having a pointer to it? The only value you could initialize such pointer to is NULL.
+0.5 for this line "you can't take the address of a reference to be able to get a pointer to it in the first place." ...And +0.5 for this "&rx takes the address of the object referred to by rx, so it's exactly the same as if you had said &x." .... beautiful explanation!
Thanks for this. I need to do exactly what you said - get a pointer to the original object through a reference.
17

The high-level concept that references implement is just another name for an existing object. You can have a pointer to an object (or function), but you can't have a pointer to an object's name. For this very reason, the idea of a pointer to a reference makes no sense. In other words, references are immaterial, in general case they simply do not exist in memory. They don't exist as something that can be pointed to.

It is true that in many cases in practice references do occupy memory (and are implemented as pointers in disguise). But that just an implementation detail specific to some particular contexts. In general case references do not occupy memory, as is explicitly stated in the language specification which immediately follows from the language specification.

37 Comments

@Nawaz : 8.3.2/3 It is unspecified whether or not a reference requires storage
@Prasoon Saurav: In my copy of the standard, 8.3.2/5 explicitly says There shall be no references to references, no arrays of references, and no pointers to references.
@Nawaz: Apparently we have different understanding of what "in general case" mean. The standard indeed says that it is unspecified whether references occupy storage. This means that one can say that "in general case, references occupy no storage" and that "in general case, references occupy storage". Both statements are true. You simply use the one that is relevant in your specific context.
@AndreyT : One can say "not all kids are nice" and "not all kids are naughty".... Trying using "in general" here... "in general, muslims are terrorists".. and "in general, muslims are not terrorists"... "in general, indians are idiots" and "in general, indians are not idiots"..."in general, you are wrong" and "in general, you are not wrong"... these are contradicting statements. Period!
@AndreyT : You said " In general case references do not occupy memory, as is explicitly stated in the language specification."... which is quite misleading. If it should make sense here, then it should make sense everywhere whenever the specification uses the term "unspecified"..... I repeat the specification says "It is unspecified whether or not a reference requires storage" which cannot be translated into "in general, references do not occupy memory".
|
5

What would be the difference between a pointer to a reference (to the object) and a pointer to the actual object? The reference cannot be changed to refer to another object. Just use a regular pointer to the object in question.

On the other hand, a reference to a pointer, like any other reference, gives you a modifiable handle to a particular variable. It happens to be a pointer in this case.

Comments

1

Because a reference is not a thing that can be pointed at, which in turn is because it does not actually have to be represented anywhere in memory. References exist to give alternate names to already-existing things. You can get a pointer to the renamed thing, but that is a pointer to a value, not a pointer to a reference.

Comments

0

Suppose "int *&X" is legal, because reference is just another name of an object, the expression is equal to "int *X" and is not useful.

1 Comment

int *&X is LEGAL, this int &*X is NOT legal

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.