Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are pointers to a reference illegal in C++?

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

like image 794
Mahesh Avatar asked Jan 08 '11 06:01

Mahesh


People also ask

Can you have a pointer to a reference?

A pointer to reference is illegal in C++, because -unlike a pointer- a reference is just a concept that allows the programmer to make aliases of something else. A pointer is a place in memory that has the address of something else, but a reference is NOT.

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.

Why are references safer than pointers?

In C++, Reference variables are safer than pointers because reference variables must be initialized and they cannot be changed to refer to something else once they are initialized.

Is pointer passed by reference C?

There is no such thing as reference in C. Passing a pointer to a function will not copy the object that the pointer is pointing to.


4 Answers

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.

like image 199
James McNellis Avatar answered Oct 17 '22 11:10

James McNellis


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.

like image 34
AnT Avatar answered Oct 17 '22 10:10

AnT


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.

like image 5
Potatoswatter Avatar answered Oct 17 '22 11:10

Potatoswatter


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.

like image 1
Karl Knechtel Avatar answered Oct 17 '22 10:10

Karl Knechtel